FLYFUN 发表于 2004-5-8 21:27:00

csharparx ....

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