public void AddBlockArray() { Database db = HostApplicationServices.WorkingDatabase; ObjectId refid; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelSpace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; refid = db.OverlayXref(@"d:\Tmp\door.dwg", "name");// 把外部文件转换为块定义 //BlockReference br = new BlockReference(Point3d.Origin, refid); // 通过块定义创建块参照 for (int i = 0; i < 3; i++) { double baseX = i * 900; for (int j = 0; j < 3; j++) { Point3d baseP = new Point3d(baseX, j * 600, 0); BlockReference br = new BlockReference(baseP, refid); // 通过块定义创建块参照 modelSpace.AppendEntity(br); //把块参照添加到块表记录 trans.AddNewlyCreatedDBObject(br, true); // 通过事务添加块参照到数据库 br.Dispose(); } } trans.Commit(); } }
// Show block in its current position protected override bool WorldDraw(WorldDraw draw) { var inMemoryBlockRef = new BlockReference(point, blockDefId); draw.Geometry.Draw(inMemoryBlockRef); inMemoryBlockRef.Dispose(); return(true); }
// Need to override this method. // We are showing our block in its current position here. //-------------------------------------------------------------- protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw) { BlockReference inMemoryBlockInsert = new BlockReference(_point, _blockId); draw.Geometry.Draw(inMemoryBlockInsert); inMemoryBlockInsert.Dispose(); return(true); } // WorldDraw()
public void BlockArrayFill() { using (MyForm autoBlockFill = new MyForm()) { autoBlockFill.ShowInTaskbar = false; Application.ShowModalDialog(autoBlockFill); if (autoBlockFill.DialogResult == System.Windows.Forms.DialogResult.OK) { //Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("矩形范围面积为:\n" + autoBlockFill.recArea); Database db = HostApplicationServices.WorkingDatabase; ObjectId refid; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord modelSpace = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; refid = db.OverlayXref(@"d:\Tmp\door.dwg", "name");// 把外部文件转换为块定义 //BlockReference br = new BlockReference(Point3d.Origin, refid); // 通过块定义创建块参照 for (int i = 0; i < 3; i++) { double baseX = i * 900; for (int j = 0; j < 3; j++) { Point3d baseP = new Point3d(autoBlockFill.pointLeftB.X + baseX, autoBlockFill.pointLeftB.Y + j * 600, 0); BlockReference br = new BlockReference(baseP, refid); // 通过块定义创建块参照 modelSpace.AppendEntity(br); //把块参照添加到块表记录 trans.AddNewlyCreatedDBObject(br, true); // 通过事务添加块参照到数据库 br.Dispose(); } } trans.Commit(); } } } }
public void Draw(string block, string Basislayer) { Database db = HostApplicationServices.WorkingDatabase; _AcDb.TransactionManager myTm = db.TransactionManager; Transaction myT = db.TransactionManager.StartTransaction(); Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ObjectContextManager conTextManager = db.ObjectContextManager; Dictionary <string, Point3d> _attPos = new Dictionary <string, Point3d>(); List <AttributeDefinition> _attDef = new List <AttributeDefinition>(); MyLayer objLayer = MyLayer.Instance; //objLayer.CheckLayer(Basislayer, true); try { using (DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument()) { //Block in Zeichnung einfügen BlockTable bt = (BlockTable)myT.GetObject(db.BlockTableId, OpenMode.ForRead); BlockTableRecord btr = (BlockTableRecord)myT.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockTableRecord btrDef = (BlockTableRecord)myT.GetObject(bt[block], OpenMode.ForRead); //Attribute aus Blockdefinition übernehmen if (btrDef.HasAttributeDefinitions) { foreach (ObjectId id in btrDef) { DBObject obj = myT.GetObject(id, OpenMode.ForRead); try { AttributeDefinition ad = (AttributeDefinition)obj; if (ad != null) { _attPos.Add(ad.Tag, ad.Position); _attDef.Add(ad); } } catch { try { Entity ent = (Entity)obj; //Layer = ent.Layer; } catch { } } } } BlockReference blkRef = new BlockReference(_Pos3d, bt[block]) { ScaleFactors = new Scale3d(db.Cannoscale.Scale), Layer = Basislayer }; btr.AppendEntity(blkRef); //XData schreiben RegAppTable acRegAppTbl; acRegAppTbl = (RegAppTable)myT.GetObject(db.RegAppTableId, OpenMode.ForRead); if (!acRegAppTbl.Has(Global.Instance.AppName)) { using (RegAppTableRecord acRegAppTblRec = new RegAppTableRecord()) { acRegAppTblRec.Name = Global.Instance.AppName; acRegAppTbl.UpgradeOpen(); acRegAppTbl.Add(acRegAppTblRec); myT.AddNewlyCreatedDBObject(acRegAppTblRec, true); } } using (ResultBuffer rb = new ResultBuffer()) { rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, Global.Instance.AppName)); rb.Add(new TypedValue((int)DxfCode.ExtendedDataWorldXCoordinate, _Pos3d)); if (_HöheOrg != null) { rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, _HöheOrg)); } blkRef.XData = rb; rb.Dispose(); } myT.AddNewlyCreatedDBObject(blkRef, true); //Attribute befüllen if (_attPos != null) { string[] lsBasislayer = Basislayer.Split('-'); string Stammlayer = lsBasislayer[lsBasislayer.Length - 2]; for (int i = 0; i < _attPos.Count; i++) { AttributeReference _attRef = new AttributeReference(); _attRef.SetDatabaseDefaults(); _attRef.SetAttributeFromBlock(_attDef[i], Matrix3d.Identity); _attRef.SetPropertiesFrom(_attDef[i]); Point3d ptBase = new Point3d(blkRef.Position.X + _attRef.Position.X, blkRef.Position.Y + _attRef.Position.Y, blkRef.Position.Z + _attRef.Position.Z); _attRef.Position = ptBase; string attLayer = String.Empty; KeyValuePair <string, Point3d> keyValuePair = _attPos.ElementAt(i); switch (keyValuePair.Key) { case "number": _attRef.TextString = _PNum; _attRef.Layer = Stammlayer + Global.Instance.LayNummer; break; case "height": if (_HöheOrg != null) { _attRef.TextString = _HöheOrg; _attRef.Layer = Stammlayer + Global.Instance.LayHöhe; } break; case "date": _attRef.Layer = Stammlayer + "-Datum"; _attRef.Layer = Stammlayer + Global.Instance.LayDatum; break; case "code": _attRef.Layer = Stammlayer + "-Code"; _attRef.Layer = Stammlayer + Global.Instance.LayCode; break; case "owner": _attRef.Layer = Stammlayer + "-Hersteller"; break; default: break; } blkRef.AttributeCollection.AppendAttribute(_attRef); myT.AddNewlyCreatedDBObject(_attRef, true); } blkRef.Dispose(); } } } //Block aus Prototypzeichnung holen catch { } finally { myT.Commit(); myT.Dispose(); } }
public void LevelLabel() { if (!CheckLicense.Check()) { return; } if (!init) { if (!ShowSettings()) { return; } } while (true) { PromptPointOptions ptOpts = new PromptPointOptions("\nKot noktası: [Reset/Güncelle]", "Reset Update"); PromptPointResult ptRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint(ptOpts); if (ptRes.Status == PromptStatus.Cancel) { return; } else if (ptRes.Status == PromptStatus.Keyword && ptRes.StringResult == "Reset") { Reset(); return; } else if (ptRes.Status == PromptStatus.Keyword && ptRes.StringResult == "Update") { PromptSelectionResult selRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetSelection(); if (selRes.Status != PromptStatus.OK) { return; } UpdateLevelBlocks(selRes.Value.GetObjectIds()); return; } else { Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; ObjectId blockId = ObjectId.Null; using (Transaction tr = db.TransactionManager.StartTransaction()) using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead)) { if (bt.Has(BlockName)) { blockId = bt[BlockName]; } tr.Commit(); } if (blockId.IsNull) { MessageBox.Show("Kot bloğu '" + BlockName + "' bulunamadı.", "Level", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string level = GetLevel(ptRes.Value); Matrix3d ucs2wcs = AcadUtility.AcadGraphics.UcsToWcs; Point3d ptWorld = ptRes.Value.TransformBy(ucs2wcs); double rotation = Math.Atan2(ucs2wcs.CoordinateSystem3d.Xaxis.Y, ucs2wcs.CoordinateSystem3d.Xaxis.X); using (Transaction tr = db.TransactionManager.StartTransaction()) using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)) { BlockReference bref = new BlockReference(ptWorld, blockId); bref.Rotation = rotation; bref.ScaleFactors = new Scale3d(BlockScale); btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); Dictionary <AttributeReference, AttributeDefinition> dict = new Dictionary <AttributeReference, AttributeDefinition>(); BlockTableRecord blockDef = tr.GetObject(blockId, OpenMode.ForRead) as BlockTableRecord; foreach (ObjectId id in blockDef) { AttributeDefinition attDef = tr.GetObject(id, OpenMode.ForRead) as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { // Create a new AttributeReference AttributeReference attRef = new AttributeReference(); dict.Add(attRef, attDef); attRef.SetAttributeFromBlock(attDef, bref.BlockTransform); attRef.TextString = level; bref.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } if (LevelJig.Jig(ptRes.Value, bref, dict)) { Line line = new Line(); line.StartPoint = ptWorld; line.EndPoint = bref.Position; btr.AppendEntity(line); tr.AddNewlyCreatedDBObject(line, true); tr.Commit(); } else { bref.Dispose(); tr.Abort(); return; } } } } }