- 积分
- 211
- 明经币
- 个
- 注册时间
- 2010-9-12
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
- ////插入块
- private static bool InsertBlock(string strBlockName, string strFilePath)
- {
- if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
- {
- return false;
- }
- Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- if (!System.IO.File.Exists(strFilePath))
- {
- CGcEditor.ShowMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
- return false;
- }
- using (Transaction tr = doc.TransactionManager.StartTransaction())
- {
- using (Database db = new Database(false, true))
- {
- db.ReadDwgFile(strFilePath, System.IO.FileShare.Read, true, null);
- Autodesk.AutoCAD.DatabaseServices.TransactionManager trMag = db.TransactionManager;
- using (Transaction trm = trMag.StartTransaction())
- {
- BlockTable sourceBlkTbl = (BlockTable)trm.GetObject(
- db.BlockTableId, OpenMode.ForRead, false);
- if (! sourceBlkTbl.Has(strBlockName))
- {
- return false;
- }
- ObjectIdCollection blockIds = new ObjectIdCollection();
- blockIds.Add(sourceBlkTbl[strBlockName]);
- IdMapping IdMap = new IdMapping();
- db.WblockCloneObjects(blockIds, doc.Database.BlockTableId, IdMap, DuplicateRecordCloning.Replace, false);
- }
- }
- tr.Commit();
- }
- return true;
- }
- public static ObjectId InsertBlock(string strFilePath, string strBlockName, Point3d insertPt)
- {
- ObjectId objId = new ObjectId();
- if (strBlockName.CompareTo("") == 0 || strFilePath.CompareTo("") == 0)
- {
- return objId;
- }
- if (!System.IO.File.Exists(strFilePath))
- {
- CGcEditor.ShowMessage(string.Format("\nInput file :{0} no existed !", strFilePath));
- return objId;
- }
- Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
- using (Transaction tr = doc.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead);
- if (! bt.Has(strBlockName))
- {
- if (! InsertBlock(strBlockName, strFilePath))
- {
- return objId;
- }
- }
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- using (BlockReference newBlkRef = new BlockReference(insertPt, bt[strBlockName]))
- {
- //Matrix3d mat3d = Matrix3d.Rotation(dbAngle, Vector3d.ZAxis, insertPt);
- //bref.TransformBy(mat3d);
- objId = btr.AppendEntity(newBlkRef);
- tr.AddNewlyCreatedDBObject(newBlkRef, true);
- }
- tr.Commit();
- tr.Dispose();
- }
- return objId;
- }
|
评分
-
查看全部评分
|