public static void ApplyAttributes(Database db, QuickTransaction tr, BlockReference bref) { if (bref == null) { return; } var _brec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead); var btrec = _brec as BlockTableRecord; if (btrec == null) { return; } if (btrec.HasAttributeDefinitions) { var atcoll = bref.AttributeCollection; foreach (var subid in btrec) { var ent = (Entity)subid.GetObject(OpenMode.ForRead); var attDef = ent as AttributeDefinition; if (attDef != null) { var attRef = new AttributeReference(); attRef.SetDatabaseDefaults(); //optional attRef.SetAttributeFromBlock(attDef, bref.BlockTransform); attRef.Position = attDef.Position.TransformBy(bref.BlockTransform); attRef.Tag = attDef.Tag; attRef.AdjustAlignment(db); atcoll.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } } }
public AcBlockRef AddBlockRef(Point3d pos, AcTransaction trans) { //Create the block reference... var blockRef = new BlockReference(pos, this.ObjectId); blockRef.SetDatabaseDefaults(); // Default/Active layer, color, ... trans.AddEntity(blockRef); // Do it before blockRef.AttributeCollection.AppendAttribute(attRef); var attrRefs = new List <AttributeReference>(); foreach (var attrDef in this.Attributes) { // this BlockDef could be initialized throught other (closed) Transaction, the same with this.Attributes var attDefObj = attrDef.GetAcObject(trans); // trans.GetObject<AttributeDefinition>(attrDef.ObjectId); // attrDef.AcObject; var attRef = new AttributeReference(); attRef.SetAttributeFromBlock(attDefObj, blockRef.BlockTransform); attRef.SetDatabaseDefaults(); if (!attDefObj.Constant) { attRef.TextString = attDefObj.TextString; } blockRef.AttributeCollection.AppendAttribute(attRef); trans.AddNewlyCreatedDBObject(attRef, true); attrRefs.Add(attRef); } return(new AcBlockRef(blockRef, trans, attrRefs)); }
/// <summary> /// Copies attributes to BlockReference from "BlockDefinition" /// </summary> /// <param name="acBlkRef"></param> /// <param name="blockDefinition"></param> /// <param name="tr"></param> private void CreateBlockRefenceAttributes(BlockReference acBlkRef, BlockTableRecord blockDefinition, Transaction tr) { // copy/create attribute references foreach (var bdEntityObjectId in blockDefinition) { var ad = tr.GetObject(bdEntityObjectId, OpenMode.ForRead) as AttributeDefinition; if (ad == null) { continue; } using (var ar = new AttributeReference()) { ar.SetDatabaseDefaults(_db); ar.SetAttributeFromBlock(ad, acBlkRef.BlockTransform); ar.TextString = ad.TextString; // set default value, copied from AttributeDefinition ar.AdjustAlignment(HostApplicationServices.WorkingDatabase); acBlkRef.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); } } }
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 draw(string block, string Basislayer) { Database db = HostApplicationServices.WorkingDatabase; Autodesk.AutoCAD.DatabaseServices.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 Prototyp.Instance.Blockname = block; if (Prototyp.Instance.OK) { 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 ü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]); blkRef.ScaleFactors = new Scale3d(db.Cannoscale.Scale); blkRef.Layer = Basislayer; btr.AppendEntity(blkRef); myT.AddNewlyCreatedDBObject(blkRef, true);; //Attribute befüllen if (_attPos != null) { 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 Stammlayer = Basislayer.Substring(0, Basislayer.Length - 2); string attLayer = String.Empty; KeyValuePair <string, Point3d> keyValuePair = _attPos.ElementAt(i); switch (keyValuePair.Key) { case "number": _attRef.TextString = _PNum; break; case "height": if (_Höhe.HasValue) { _attRef.TextString = _Höhe.Value.ToString(); } break; default: break; } blkRef.AttributeCollection.AppendAttribute(_attRef); myT.AddNewlyCreatedDBObject(_attRef, true); } } } } } //Block aus Prototypzeichnung holen catch (Autodesk.AutoCAD.Runtime.Exception e) { } finally { myT.Commit(); myT.Dispose(); } }
public void MakeCoordGrid() { if (!CheckLicense.Check()) { return; } Autodesk.AutoCAD.ApplicationServices.Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database; ObjectId textStyleId = ObjectId.Null; using (Transaction tr = db.TransactionManager.StartTransaction()) using (TextStyleTable tt = (TextStyleTable)tr.GetObject(db.TextStyleTableId, OpenMode.ForRead)) { if (tt.Has(TextStyleName)) { textStyleId = tt[TextStyleName]; } tr.Commit(); } ObjectId textLayerId = AcadUtility.AcadEntity.GetOrCreateLayer(db, TextLayerName, Color.FromColorIndex(ColorMethod.ByAci, 1)); ObjectId lineLayerId = AcadUtility.AcadEntity.GetOrCreateLayer(db, LineLayerName, Color.FromColorIndex(ColorMethod.ByAci, 3)); ObjectId blockId = GetOrCreateBlock(db, BlockName, textLayerId, lineLayerId, textStyleId); Matrix3d ucs2wcs = AcadUtility.AcadGraphics.UcsToWcs; Matrix3d wcs2ucs = AcadUtility.AcadGraphics.WcsToUcs; // Pick polyline PromptPointResult ptRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetPoint("\nKöşe noktası: "); if (PolylineJig.Jig(ptRes.Value, out Point3dCollection points)) { int xmin = int.MaxValue; int xmax = int.MinValue; int ymin = int.MaxValue; int ymax = int.MinValue; for (int i = 0; i < 4; i++) { Point3d pt = points[i]; xmin = Math.Min(xmin, (int)pt.X); xmax = Math.Max(xmax, (int)pt.X); ymin = Math.Min(ymin, (int)pt.Y); ymax = Math.Max(ymax, (int)pt.Y); } // Interval PromptIntegerOptions intOpts = new PromptIntegerOptions("\nAralık: "); intOpts.AllowNegative = false; intOpts.AllowZero = false; intOpts.AllowNone = false; intOpts.DefaultValue = Properties.Settings.Default.Command_COORDGRID_Interval; intOpts.UseDefaultValue = true; PromptIntegerResult intRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetInteger(intOpts); if (intRes.Status == PromptStatus.OK) { Interval = intRes.Value; } else { return; } // Round limits to multiples of the interval xmin = (int)Math.Floor((double)xmin / Interval) * Interval; xmax = (int)Math.Ceiling((double)xmax / Interval) * Interval; ymin = (int)Math.Floor((double)ymin / Interval) * Interval; ymax = (int)Math.Ceiling((double)ymax / Interval) * Interval; // Text height PromptDoubleOptions thOpts = new PromptDoubleOptions("\nYazı yüksekliği: "); thOpts.AllowNegative = false; thOpts.AllowZero = false; thOpts.AllowNone = false; thOpts.DefaultValue = Properties.Settings.Default.Command_COORDGRID_TextHeight; thOpts.UseDefaultValue = true; PromptDoubleResult thRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.GetDouble(thOpts); if (thRes.Status == PromptStatus.OK) { TextHeight = thRes.Value; } else { return; } // Save settings Properties.Settings.Default.Command_COORDGRID_TextHeight = TextHeight; Properties.Settings.Default.Command_COORDGRID_Interval = Interval; Properties.Settings.Default.Save(); // Print grid NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); nfi.NumberGroupSeparator = " "; nfi.NumberDecimalDigits = 0; using (Transaction tr = db.TransactionManager.StartTransaction()) using (BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead)) using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite)) { BlockTableRecord blockDef = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead); for (int x = xmin; x <= xmax; x += Interval) { for (int y = ymin; y <= ymax; y += Interval) { Point3d v = new Point3d(x, y, 0); if (!PolylineContains(points, v)) { continue; } BlockReference blockRef = AcadUtility.AcadEntity.CreateBlockReference(db, blockId, v, TextHeight, 0); btr.AppendEntity(blockRef); tr.AddNewlyCreatedDBObject(blockRef, true); // Set attributes foreach (ObjectId id in blockDef) { AttributeDefinition attDef = tr.GetObject(id, OpenMode.ForRead) as AttributeDefinition; if (attDef != null) { using (AttributeReference attRef = new AttributeReference()) { attRef.SetDatabaseDefaults(db); attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform); blockRef.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); attRef.TextString = attDef.Tag == "X" ? x.ToString("n", nfi) : y.ToString("n", nfi); attRef.AdjustAlignment(db); } } } } } tr.Commit(); } } }