www.pudn.com > Class1.zip > Class1.cs, change:2013-11-27,size:5843b
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.LayerManager; using System.Runtime.InteropServices; using Autodesk.AutoCAD.ApplicationServices; namespace taokuang { public class Class1 { [CommandMethod("ck")] public static void InsertBlock() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; int x, y; Point3d insertPt = new Point3d(x, y, 0); ObjectId obid = InsertBlock("G:\\拉萨\\图框.dwg", "套图框", insertPt, 1); } private static bool InsertBlock(string strBlockName, string strFilePath) { Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()) { using (Transaction tr = doc.TransactionManager.StartTransaction()) { using (Database db = new Database(false, true)) { db.ReadDwgFile(strFilePath, System.IO.FileShare.Read, true, null); using (Transaction trm = db.TransactionManager.StartTransaction()) { BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(db.BlockTableId, OpenMode.ForRead, false); if (!sourceBlkTbl.Has(strBlockName)) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 91"); return false; } ObjectIdCollection blockIds = new ObjectIdCollection(); blockIds.Add(sourceBlkTbl[strBlockName]); IdMapping IdMap = new IdMapping(); db.WblockCloneObjects(blockIds, doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace, false); trm.Commit(); } } tr.Commit(); } } return true; } public static ObjectId InsertBlock(string strFilePath, string strBlockName, Point3d insertPt, int scale) { ObjectId objId = new ObjectId(); Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 118!"); return objId; } if (!System.IO.File.Exists(strFilePath)) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("没有找到外部引用文件!"); doc.Editor.WriteMessage(string.Format("\nInput file :{0} no existed !", strFilePath)); return objId; } using (Transaction tr = doc.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead); if (!bt.Has(strBlockName)) { if (!InsertBlock(strBlockName, strFilePath)) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("line 137"); return objId; } } BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); using (BlockReference newBlkRef = new BlockReference(insertPt, bt[strBlockName])) { newBlkRef.ScaleFactors = new Scale3d(scale); objId = btr.AppendEntity(newBlkRef); #region 加入默认属性 BlockTableRecord btrBlock = tr.GetObject(bt[strBlockName], OpenMode.ForRead) as BlockTableRecord; if (btrBlock.HasAttributeDefinitions) { foreach (ObjectId id in btrBlock) { DBObject ent = tr.GetObject(id, OpenMode.ForRead) as DBObject; if (ent is AttributeDefinition) { AttributeDefinition attdef = ent as AttributeDefinition;//取得块的属性定义 AttributeReference attref = new AttributeReference();//新建属性参照 attref.SetPropertiesFrom(attdef); attref.SetAttributeFromBlock(attdef, Matrix3d.Displacement(btrBlock.Origin.GetVectorTo(insertPt)));//复制属性 newBlkRef.AttributeCollection.AppendAttribute(attref);//附着属性参照 tr.AddNewlyCreatedDBObject(attref, true); attref = null; } } } #endregion tr.AddNewlyCreatedDBObject(newBlkRef, true); } tr.Commit(); //tr.Dispose(); } return objId; } } }