/// <summary> /// 通过块Id来克隆块 /// </summary> /// <param name="BlockId"></param> /// <param name="name"></param> /// <returns></returns> public ObjectId CreateBlockByClone(ObjectId BlockId) { try { using (Transaction tran = database.TransactionManager.StartTransaction()) { BlockTableRecord SourceBtr = BlockId.GetObject(OpenMode.ForRead) as BlockTableRecord; BlockTbl.UpgradeOpen();//提升权限 BlockTableRecord newBlock = new BlockTableRecord(); newBlock.Name = SourceBtr.Name; ObjectId NewBlockId = BlockTbl.Add(newBlock); tran.AddNewlyCreatedDBObject(newBlock, true); BlockTbl.DowngradeOpen();//降低权限 BlockHelper blockHelper = new BlockHelper(dwgHelper, newBlock); blockHelper.CloneEntitiesFromBlock(BlockId); tran.Commit(); return(NewBlockId); } } catch (Exception ex) { Logger.log("CreateBlockByClone", ex.Message); } return(ObjectId.Null); }
/// <summary> /// Создание определения блока панели по описанию из базы XML от конструкторов. /// Должна быть открыта транзакция. /// </summary> /// <exception cref="Autodesk.AutoCAD.Runtime.Exception">DuplicateBlockName</exception> /// <returns>ObjectId созданного определения блока в текущей базе.</returns> public void CreateBlock() { // Имя для блока панели АКР BlNameAkr = defineBlockPanelAkrName(); Openings = new List <Extents3d>(); Database db = Service.Db; //Transaction t = db.TransactionManager.TopTransaction; using (var t = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; // Ошибка если блок с таким именем уже есть if (bt.Has(BlNameAkr)) { IdBtrPanel = bt[BlNameAkr]; Inspector.AddError($"Блок панели с именем {BlNameAkr} уже определен в чертеже.", icon: System.Drawing.SystemIcons.Error); return; } BlockTableRecord btrPanel = new BlockTableRecord(); btrPanel.Name = BlNameAkr; bt.UpgradeOpen(); IdBtrPanel = bt.Add(btrPanel); bt.DowngradeOpen(); t.AddNewlyCreatedDBObject(btrPanel, true); //корректировка точки отсчета панели correctStartPointCoordinatesPanel(); // Добавление полилинии контура createContour(btrPanel, t); // Добавление окон addWindows(btrPanel, t); // заполнение плиткой addTiles(btrPanel, t); // Добавление торцов (Cheek) addCheek(btrPanel, t); // Образмеривание (на Фасаде) DimensionFacade dimFacade = new DimensionFacade(btrPanel, t, this); dimFacade.Create(); // Образмеривание (в Форме) DimensionForm dimForm = new DimensionForm(btrPanel, t, this); dimForm.Create(); t.Commit(); } }
public void ReplaceBlockWithFileStart() { Application.SetSystemVariable("FILEDIA", 1); var editorUtility = new EditorHelper(Application.DocumentManager.MdiActiveDocument); PromptEntityResult blockToReplaceResult = editorUtility.PromptForObject("Select the block to replace : ", typeof(BlockReference), false); if (blockToReplaceResult.Status != PromptStatus.OK) { return; } var openFileDialog = new System.Windows.Forms.OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } if (!File.Exists(openFileDialog.FileName)) { _ed.WriteMessage("File does not exists."); return; } if (!openFileDialog.FileName.EndsWith(".dwg", StringComparison.InvariantCultureIgnoreCase)) { _ed.WriteMessage("File is not a DWG."); return; } Point3d selectedEntityPoint; using (Transaction transaction = _db.TransactionManager.StartTransaction()) { BlockTable blockTable = (BlockTable)transaction.GetObject(_db.BlockTableId, OpenMode.ForWrite); Entity selectedEntity = (Entity)transaction.GetObject(blockToReplaceResult.ObjectId, OpenMode.ForWrite); selectedEntityPoint = ((BlockReference)selectedEntity).Position; selectedEntity.Erase(); blockTable.DowngradeOpen(); blockTable.Dispose(); transaction.Commit(); } ReplaceBlockRefWithDWG(_doc, openFileDialog.FileName, selectedEntityPoint, _ed.CurrentUserCoordinateSystem); _logger.Info(MethodBase.GetCurrentMethod().Name); }
/// <summary> /// 创建一个块表记录并添加到数据库中 /// </summary> /// <param name="db">数据库对象</param> /// <param name="blockName">块名</param> /// <param name="ents">加入块中的实体列表</param> /// <returns>返回块表记录的Id</returns> public static ObjectId AddBlockTableRecord(this Database db, string blockName, List <Entity> ents) { //打开块表 BlockTable bt = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead); if (!bt.Has(blockName)) //判断是否存在名为blockName的块 { //创建一个BlockTableRecord类的对象,表示所要创建的块 BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName;//设置块名 //将列表中的实体加入到新建的BlockTableRecord对象 ents.ForEach(ent => btr.AppendEntity(ent)); bt.UpgradeOpen(); //切换块表为写的状态 bt.Add(btr); //在块表中加入blockName块 db.TransactionManager.AddNewlyCreatedDBObject(btr, true); //通知事务处理 bt.DowngradeOpen(); //为了安全,将块表状态改为读 } return(bt[blockName]); //返回块表记录的Id }
/// <summary> 创建块定义 </summary> public static ObjectId CreateBTR(Database db, string blockName, IEnumerable <Entity> ents) { BlockTable blockTable = (BlockTable)db.BlockTableId.GetObject(OpenMode.ForRead); if (!blockTable.Has(blockName)) { var btr = new BlockTableRecord { Name = blockName }; //将列表中的实体加入到新建的BlockTableRecord foreach (var ent in ents) { btr.AppendEntity(ent); } blockTable.UpgradeOpen(); //切换块为写的状态 blockTable.Add(btr); db.TransactionManager.AddNewlyCreatedDBObject(btr, true); blockTable.DowngradeOpen(); //切换块为读的状态 } return(blockTable[blockName]); }
// Thêm 1 block chứa bản vẽ của 1 file dwg vào bản vẽ hiện tại (có scale riêng) public static bool InsertDrawing(string partDwg, Scale3d scale, Point3d ipt, double angle, out ObjectId objectId) { Document doc = AcAp.DocumentManager.MdiActiveDocument; Database curdb = doc.Database; // Biến database của bản vẽ hện tại Editor ed = doc.Editor; string blockname = Path.GetFileNameWithoutExtension(partDwg); bool result = false; objectId = ObjectId.Null; using (DocumentLock loc = doc.LockDocument()) { ObjectId blkid = ObjectId.Null; // Biến lấy ID của file đọc vào try { using (Database db = new Database(false, true)) // Biến lấy database của file nạp vào { db.ReadDwgFile(partDwg, System.IO.FileShare.Read, true, ""); // Lấy database blkid = curdb.Insert(partDwg, db, true); // Lấy ID } } catch (IOException) { return(false); } using (Transaction tr = doc.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(curdb.BlockTableId, OpenMode.ForRead); if (!bt.Has(blockname)) { bt.UpgradeOpen(); BlockTableRecord btrec = blkid.GetObject(OpenMode.ForRead) as BlockTableRecord; btrec.UpgradeOpen(); //nâng cấp để ghi btrec.Name = blockname; //thêm tên btrec.DowngradeOpen(); //hạ cấp, không cho ghi nữa bt.DowngradeOpen(); } blkid = bt[blockname]; //Thêm các hình vẽ ở bản vẽ cũ vào using (BlockTableRecord btr = (BlockTableRecord)curdb.CurrentSpaceId.GetObject(OpenMode.ForWrite)) { //Insert vật thể using (BlockReference bref = new BlockReference(ipt, blkid)) { Matrix3d mat = Matrix3d.Identity; bref.TransformBy(mat); bref.ScaleFactors = scale; bref.Rotation = angle; btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); bref.DowngradeOpen(); objectId = bref.ObjectId; } } //Tạo lại bản vẽ??? ed.Regen(); tr.Commit(); result = true; } } return(result); }
public void drawTable(Database db, Transaction trans, Point3d insertPoint) { BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead); bt.UpgradeOpen(); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); Polyline pHorizonline1 = new Polyline(); pHorizonline1.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y), new Point2d(insertPoint.X + 30, insertPoint.Y)); btr.AppendEntity(pHorizonline1); trans.AddNewlyCreatedDBObject(pHorizonline1, true); Polyline pHorizonline2 = new Polyline(); pHorizonline2.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y - 8.5), new Point2d(insertPoint.X + 30, insertPoint.Y - 8.5)); btr.AppendEntity(pHorizonline2); trans.AddNewlyCreatedDBObject(pHorizonline2, true); Polyline pHorizonline3 = new Polyline(); pHorizonline3.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y - 19), new Point2d(insertPoint.X + 30, insertPoint.Y - 19)); btr.AppendEntity(pHorizonline3); trans.AddNewlyCreatedDBObject(pHorizonline3, true); Polyline pHorizonline4 = new Polyline(); pHorizonline4.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y - 32.5), new Point2d(insertPoint.X + 247, insertPoint.Y - 32.5)); btr.AppendEntity(pHorizonline4); trans.AddNewlyCreatedDBObject(pHorizonline4, true); Polyline pHorizonline5 = new Polyline(); pHorizonline5.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y - 41), new Point2d(insertPoint.X + 247, insertPoint.Y - 41)); btr.AppendEntity(pHorizonline5); trans.AddNewlyCreatedDBObject(pHorizonline5, true); Polyline pVerticalLine1 = new Polyline(); pVerticalLine1.CreatePolyline(new Point2d(insertPoint.X, insertPoint.Y), new Point2d(insertPoint.X, insertPoint.Y - 41)); btr.AppendEntity(pVerticalLine1); trans.AddNewlyCreatedDBObject(pVerticalLine1, true); Polyline pVerticalLine2 = new Polyline(); pVerticalLine2.CreatePolyline(new Point2d(insertPoint.X + 30, insertPoint.Y), new Point2d(insertPoint.X + 30, insertPoint.Y - 41)); btr.AppendEntity(pVerticalLine2); trans.AddNewlyCreatedDBObject(pVerticalLine2, true); DBText TStationName = new DBText(); TStationName.TextString = "站名"; TStationName.Height = 3; TStationName.VerticalMode = TextVerticalMode.TextVerticalMid; TStationName.HorizontalMode = TextHorizontalMode.TextCenter; TStationName.AlignmentPoint = new Point3d(insertPoint.X + 30 / 2, insertPoint.Y - 8.5 / 2, 0); DBText TStationmark = new DBText(); TStationmark.TextString = "图示"; TStationmark.Height = 3; TStationmark.VerticalMode = TextVerticalMode.TextVerticalMid; TStationmark.HorizontalMode = TextHorizontalMode.TextCenter; TStationmark.AlignmentPoint = new Point3d(insertPoint.X + 30 / 2, insertPoint.Y - 8.5 - 8.5 / 2, 0); DBText TStationLocation = new DBText(); TStationLocation.TextString = "站中心里程"; TStationLocation.Height = 3; TStationLocation.VerticalMode = TextVerticalMode.TextVerticalMid; TStationLocation.HorizontalMode = TextHorizontalMode.TextCenter; TStationLocation.AlignmentPoint = new Point3d(insertPoint.X + 30 / 2, insertPoint.Y - (19 + (32.5 - 19) / 2), 0); DBText TStationDistance = new DBText(); TStationDistance.TextString = "站间距离"; TStationDistance.Height = 3; TStationDistance.VerticalMode = TextVerticalMode.TextVerticalMid; TStationDistance.HorizontalMode = TextHorizontalMode.TextCenter; TStationDistance.AlignmentPoint = new Point3d(insertPoint.X + 30 / 2, insertPoint.Y - (32.5 + (41 - 32.5) / 2), 0); //db.AddToModelSpace(pHorizonline1, pHorizonline2, pHorizonline3, pHorizonline4, pHorizonline5, pVerticalLine1, pVerticalLine2); db.AddToModelSpace(TStationName, TStationmark, TStationLocation, TStationDistance); bt.DowngradeOpen(); btr.DowngradeOpen(); }
//如果不存在则添加铁轨标识 private void CreateRailWayMark(Database db, Transaction trans, Point3d insertPoint, string RailWayDirection) { // Open the Block table for read BlockTable acBlkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec = new BlockTableRecord(); acBlkTbl.UpgradeOpen(); acBlkTbl.Add(acBlkTblRec); acBlkTblRec.Name = "铁轨_Length_208"; //块属性 double textHeight = 3; //AttributeDefinition ProjectNameShortAtt = new AttributeDefinition(new Point3d(insertPoint.X, insertPoint.Y - 7 / 2, 0), RailWayDirection, "上/下行", "输入线路名简称", ObjectId.Null); AttributeDefinition ProjectNameShortAtt = new AttributeDefinition(); ProjectNameShortAtt.TextString = "XXX上行/下行线"; ProjectNameShortAtt.Tag = "上行/下行线"; ProjectNameShortAtt.Prompt = "输入线路名称"; ProjectNameShortAtt.TextStyleId = ObjectId.Null; ProjectNameShortAtt.Justify = AttachmentPoint.MiddleLeft; ProjectNameShortAtt.HorizontalMode = TextHorizontalMode.TextLeft; ProjectNameShortAtt.VerticalMode = TextVerticalMode.TextVerticalMid; ProjectNameShortAtt.AlignmentPoint = new Point3d(insertPoint.X, insertPoint.Y - textHeight / 2, 0); SetStyleForAttribut(ProjectNameShortAtt, textHeight, false); acBlkTblRec.AppendEntity(ProjectNameShortAtt); trans.AddNewlyCreatedDBObject(ProjectNameShortAtt, true); //ProjectNameShortAtt.AlignmentPoint = new Point3d(1, 1, 0); Polyline BigRectangle = new Polyline(4); BigRectangle.AddVertexAt(0, new Point2d(insertPoint.X + 15, insertPoint.Y), 0, 0.1, 0.1); BigRectangle.AddVertexAt(1, new Point2d(insertPoint.X + 15 + 228, insertPoint.Y), 0, 0.1, 0.1); BigRectangle.AddVertexAt(2, new Point2d(insertPoint.X + 15 + 228, insertPoint.Y - 7), 0, 0.1, 0.1); BigRectangle.AddVertexAt(3, new Point2d(insertPoint.X + 15, insertPoint.Y - 7), 0, 0.1, 0.1); BigRectangle.Closed = true; acBlkTblRec.AppendEntity(BigRectangle); trans.AddNewlyCreatedDBObject(BigRectangle, true); Polyline InsideRectangle_1 = new Polyline(4); InsideRectangle_1.AddVertexAt(0, new Point2d(insertPoint.X + 65, insertPoint.Y), 0, 0.1, 0.1); InsideRectangle_1.AddVertexAt(1, new Point2d(insertPoint.X + 110, insertPoint.Y), 0, 0.1, 0.1); InsideRectangle_1.AddVertexAt(2, new Point2d(insertPoint.X + 110, insertPoint.Y - 7), 0, 0.1, 0.1); InsideRectangle_1.AddVertexAt(3, new Point2d(insertPoint.X + 65, insertPoint.Y - 7), 0, 0.1, 0.1); InsideRectangle_1.Closed = true; acBlkTblRec.AppendEntity(InsideRectangle_1); trans.AddNewlyCreatedDBObject(InsideRectangle_1, true); Polyline InsideRectangle_2 = new Polyline(4); InsideRectangle_2.AddVertexAt(0, new Point2d(insertPoint.X + 155, insertPoint.Y), 0, 0.1, 0.1); InsideRectangle_2.AddVertexAt(1, new Point2d(insertPoint.X + 200, insertPoint.Y), 0, 0.1, 0.1); InsideRectangle_2.AddVertexAt(2, new Point2d(insertPoint.X + 200, insertPoint.Y - 7), 0, 0.1, 0.1); InsideRectangle_2.AddVertexAt(3, new Point2d(insertPoint.X + 155, insertPoint.Y - 7), 0, 0.1, 0.1); InsideRectangle_2.Closed = true; acBlkTblRec.AppendEntity(InsideRectangle_2); trans.AddNewlyCreatedDBObject(InsideRectangle_2, true); Hatch hatch = new Hatch(); acBlkTblRec.AppendEntity(hatch); trans.AddNewlyCreatedDBObject(hatch, true); hatch.SetDatabaseDefaults(); hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); hatch.Associative = true; ObjectIdCollection ids = new ObjectIdCollection(); ids.Add(InsideRectangle_1.ObjectId); ObjectIdCollection ids1 = new ObjectIdCollection(); ids1.Add(InsideRectangle_2.ObjectId); hatch.AppendLoop(HatchLoopTypes.Default, ids); hatch.AppendLoop(HatchLoopTypes.Default, ids1); hatch.EvaluateHatch(true); db.TransactionManager.AddNewlyCreatedDBObject(acBlkTblRec, true); //acBlkTblRec.ObjectId.AddAttsToBlock(ProjectNameShortAtt); //Dictionary<string,string> att=new Dictionary<string,string>(); //att.Add("xx上行线/下行线","吉珲上行线"); //double textHeight=50; //db.CurrentSpaceId.InsertBlockReference("0", "铁轨_Length_248", new Point3d(insertPoint.X, insertPoint.Y - textHeight / 2, 0), new Scale3d(2), 0, att); acBlkTbl.DowngradeOpen(); }