csharparx ....
using System;<BR>using Autodesk.AutoCAD.DatabaseServices;<BR>using Autodesk.AutoCAD.Runtime;<BR>using Autodesk.AutoCAD.Geometry;<BR>using Autodesk.AutoCAD.ApplicationServices;<BR>using System.Reflection;<BR>using System.IO;<BR>using System.Collections;<BR>using System.Runtime.InteropServices;<BR>using Autodesk.AutoCAD.Interop;<BR>using Autodesk.AutoCAD.Interop.Common;<BR>using System.Diagnostics;<BR>using Autodesk.AutoCAD.PlottingServices;<BR>using Autodesk.AutoCAD.Colors;<BR>using DBTransMan = Autodesk.AutoCAD.DatabaseServices.TransactionManager;namespace setenv<BR>{<BR> public class entites<BR> {<BR> private Database db;<BR><BR> public entites()<BR> {<BR> db= Application.DocumentManager.MdiActiveDocument.Database;<BR> }<BR> //创建多义线<BR> public bool createPolyline(Point3dCollection ptArr,string lyrname)<BR> {<BR> DBTransMan tm= db.TransactionManager;<BR> Transaction myT = tm.StartTransaction();
BlockTable bt=(BlockTable)tm.GetObject(db.BlockTableId, OpenMode.ForRead, false);<BR> BlockTableRecord btr = (BlockTableRecord)tm.GetObject(bt, OpenMode.ForWrite,false);<BR> try<BR> {<BR> DoubleCollection blgs=new DoubleCollection();<BR> for(int i=1;i<=ptArr.Count;i++)<BR> blgs.Add(0.0);<BR> Polyline2d pline=new Polyline2d(Poly2dType.SimplePoly,ptArr,0.0,false,0.0,0.0,blgs);<BR> pline.Layer=lyrname;<BR> btr.AppendEntity(pline);<BR> tm.AddNewlyCreatedDBObject(pline, true);<BR> myT.Commit();<BR> }<BR> finally<BR> {<BR> bt.Close();<BR> btr.Close();<BR> myT.Dispose();<BR> }<BR> return true;<BR> }<BR> //创建新层<BR> public bool createNewLayer(string lyrname,string ltname,short lyrcolor)<BR> {<BR> ObjectId objId=db.LayerTableId ;<BR> LayerTable lyrTb=(LayerTable)objId.Open(OpenMode.ForWrite);<BR> try<BR> {<BR> if (!lyrTb.Has(lyrname))<BR> {<BR> LayerTableRecord lyrTbr=new LayerTableRecord();<BR> lyrTbr.Name=lyrname;<BR> lyrTbr.IsFrozen=false;<BR> lyrTbr.IsOff=false;<BR> lyrTbr.ViewportVisibilityDefault =true;<BR> lyrTbr.IsLocked=false;<BR> Color color1=new Color() ;<BR> color1.ColorIndex =lyrcolor;<BR> lyrTbr.Color=color1;<BR> lyrTbr.LinetypeObjectId =getLineTypeId(ltname);<BR> lyrTb.Add(lyrTbr);<BR> lyrTbr.Close();<BR> }<BR> }<BR> finally<BR> {<BR> lyrTb.Close();<BR> <BR> }<BR> return true;<BR> }<BR> //获取ltname 线型ID<BR> private ObjectId getLineTypeId(string ltname)<BR> {<BR> ObjectId objId=db.LinetypeTableId ;<BR> ObjectId id1=new ObjectId();<BR> LinetypeTable ltTb=(LinetypeTable)objId.Open(OpenMode.ForRead);<BR> if(!ltTb.Has(ltname))<BR> id1= ltTb["CONTINUOUS"];<BR> else<BR> id1= ltTb;<BR> ltTb.Close();<BR> return id1;<BR> }
//<BR> protected void Dispose( )<BR> {<BR> //<BR> }
}
}
调用
<BR> public static void mycmd1()<BR> {<BR> Point3dCollection ptArr=new Point3dCollection();<BR> Point3d pt1=new Point3d(0,0,0);<BR> Point3d pt2=new Point3d(1,1,0);<BR> Point3d pt3=new Point3d(2,2,0);<BR> ptArr.Add(pt1);<BR> ptArr.Add(pt2);<BR> ptArr.Add(pt3);<BR> string lyrname="0";<BR> <BR> entites ets=new entites();<BR> ets.createPolyline(ptArr,lyrname);<BR> ets.createNewLayer("1","HIDDEN",2);<BR> <BR> }
页:
[1]