open Autodesk.AutoCAD.ApplicationServices
open Autodesk.AutoCAD.Runtime
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.Geometry
let (|AcEnt|AcNum|AcUnknow|) (x:TypedValue)=
enum<LispDataType>(x.TypeCode|>int)|>function
|LispDataType.ObjectId->AcEnt(x.Value:?>ObjectId)
|LispDataType.Int16->AcNum(x.Value:?>int16|>float)
|LispDataType.Int32->AcNum(x.Value:?>int|>float)
|LispDataType.Double->AcNum(x.Value:?>float)
|_->AcUnknow
let Rb=function
|null->[]
|(rb:ResultBuffer)->rb.AsArray()|>Array.toList
let Init()=
let doc=Application.DocumentManager.MdiActiveDocument
doc,doc.Editor,doc.Database,doc.TransactionManager.StartTransaction
[<LispFunction "NAREZ">]
let Narez arg=
let doc,ed,db,trf=Init()
let MakeRegion (pt1:Point3d) width height=
let cl=new DBObjectCollection()
new Polyline2d(Poly2dType.SimplePoly,new Point3dCollection([|pt1
new Point3d(pt1.X+width,pt1.Y,pt1.Z)
new Point3d(pt1.X+width,pt1.Y+height,pt1.Z)
new Point3d(pt1.X,pt1.Y+height,pt1.Z)|]),
0.0,true,0.0,0.0,new DoubleCollection([|0.0;0.0;0.0;0.0|]))|>cl.Add|>ignore
Region.CreateFromCurves(cl).[0]:?>Region
arg|>Rb|>function
|[AcEnt id;AcNum width;AcNum height]->
let start=System.DateTime.Now
use tr=trf()
tr.GetObject(id,OpenMode.ForWrite)|>function
| :? Region as rg->let ext=rg.GeometricExtents
let block=tr.GetObject(rg.BlockId,OpenMode.ForWrite):?>BlockTableRecord
let AppendEnt ent=
block.AppendEntity ent|>ignore
tr.AddNewlyCreatedDBObject(ent,true)
let obr=MakeRegion ext.MinPoint width height
let mtw=Matrix3d.Displacement(new Vector3d(width,0.0,0.0))
let mth=Matrix3d.Displacement(new Vector3d(-width*(((ext.MaxPoint.X-ext.MinPoint.X)/width|>int)+1|>float),height,0.0))
{0..(ext.MaxPoint.Y-ext.MinPoint.Y)/height|>int}
|>Seq.iter (fun _ ->{0..(ext.MaxPoint.X-ext.MinPoint.X)/width|>int}
|>Seq.fold (fun (next,check) _ ->
let cl=(obr.Clone():?>Region)
obr.TransformBy(mtw)
let pc=new Point3dCollection()
cl.IntersectWith(rg,Intersect.ExtendThis,pc,0,0)
let inters()=cl.BooleanOperation(BooleanOperationType.BoolIntersect,rg.Clone():?>Region)
cl.Area<0.0000001
(pc.Count>0,next,check)|>function
|true,_,_ when inters()->cl.Dispose()
(false,true)
|true,_,_->AppendEnt cl
(false,true)
|_,_,true when inters()->cl.Dispose()
(false,false)
|_,_,true|_,true,_->AppendEnt cl
(true,false)
|_->cl.Dispose()
(false,false))
(false,false)|>ignore
obr.TransformBy(mth))
obr.Dispose()
|_->ed.WriteMessage("Работаем только с Region'ами...\n")
tr.Commit()
"Затраченное время: "+string(System.DateTime.Now-start)+"\n"|>ed.WriteMessage
|_->ed.WriteMessage("Неверный тип аргументов - пример (narez (entlast) 10 10)\n")