/// <summary> /// Вставка вхождения блока /// </summary> public static BlockReference InsertBlockRef( ObjectId btrId, Point3d pt, [NotNull] BlockTableRecord owner, [NotNull] Transaction t, double scale = 1) { var db = owner.Database; var blRef = new BlockReference(pt, btrId) { Position = pt }; if (blRef.Annotative == AnnotativeStates.True) { // Установка аннотативного масштаба blRef.AddContext(db.Cannoscale); } else if (Math.Abs(scale - 1) > 0.001) { blRef.TransformBy(Matrix3d.Scaling(scale, pt)); } owner.AppendEntity(blRef); t.AddNewlyCreatedDBObject(blRef, true); AddAttributes(blRef, btrId.GetObjectT <BlockTableRecord>(), t); return(blRef); }
public static ObjectId AppendBlockItem(Point3d insertPoint, ObjectId blockTableRecordId, List <string> attrTextValues, Matrix3d toWcsTransform) { ObjectId resBlockId = ObjectId.Null; Tools.StartTransaction(() => { Transaction trans = Tools.GetTopTransaction(); // Add a block reference to the model space BlockTableRecord ms = Tools.GetAcadBlockTableRecordModelSpace(OpenMode.ForWrite); BlockTableRecord btr = blockTableRecordId.GetObjectForRead <BlockTableRecord>(); BlockReference br = new BlockReference(insertPoint, blockTableRecordId); br.SetDatabaseDefaults(); br.TransformBy(toWcsTransform); ObjectContextManager ocm = btr.Database.ObjectContextManager; ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (btr.Annotative == AnnotativeStates.True) { br.AddContext(occ.CurrentContext); } resBlockId = ms.AppendEntity(br); trans.AddNewlyCreatedDBObject(br, true); // Add attributes from the block table record List <AttributeDefinition> attributes = GetAttributes(btr, trans); int i = 0; foreach (AttributeDefinition acAtt in attributes) { if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform); if (attrTextValues != null) { acAttRef.TextString = attrTextValues[i++]; } else { acAttRef.TextString = acAtt.TextString; } br.AttributeCollection.AppendAttribute(acAttRef); trans.AddNewlyCreatedDBObject(acAttRef, true); } } } br.RecordGraphicsModified(true); }); return(resBlockId); }
public static ObjectId InsertBlock(Point3d anchorPoint, double Rotation, ObjectId BlockID) { // Get the current document and database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES"); Transaction acTrans = acDoc.TransactionManager.TopTransaction; // Open the Block table for read BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord blockDef = BlockID.GetObject(OpenMode.ForRead) as BlockTableRecord; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Insert the block into the current space using (BlockReference acBlkRef = new BlockReference(anchorPoint, BlockID)) { Matrix3d curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem; CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d; acBlkRef.TransformBy(Matrix3d.Rotation(Rotation, curUCS.Zaxis, anchorPoint)); //For some unknown reason this is needed in Civil3D //TODO: Find reason below line is needed and fix it acBlkRef.TransformBy(Matrix3d.Scaling(1000, anchorPoint)); acBlkRef.AddContext(occ.GetContext("1:1")); acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acBlkTblRec.AppendEntity(acBlkRef); acTrans.AddNewlyCreatedDBObject(acBlkRef, true); // AttributeDefinitions foreach (ObjectId id in blockDef) { DBObject obj = id.GetObject(OpenMode.ForRead); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { //This is a non-constant AttributeDefinition //Create a new AttributeReference using (AttributeReference attRef = new AttributeReference()) { attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform); attRef.TextString = "0"; //Add the AttributeReference to the BlockReference acBlkRef.AttributeCollection.AppendAttribute(attRef); acTrans.AddNewlyCreatedDBObject(attRef, true); } } } return(acBlkRef.ObjectId); } }
public static void TagFoundations(List <Curve> lines, Plot p) { Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database; ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES"); using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; ObjectId current = acCurDb.Clayer; acCurDb.Clayer = acLyrTbl[Main.FoundationTextLayer]; // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId = ObjectId.Null; if (!acBlkTbl.Has("FormationTag")) { Main.LoadBlocks(); } foreach (Curve c in lines) { Matrix3d curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem; CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d; //Get lable point Point3d labelPoint3d = c.GetPointAtDist(c.GetDistanceAtParameter(c.EndParam) / 2); Point3d labelPoint = new Point3d(labelPoint3d.X, labelPoint3d.Y, 0); // Insert the block into the current space using (BlockReference acBlkRef = new BlockReference(labelPoint, acBlkTbl["FormationTag"])) { //Calculate Line Angle double y = c.EndPoint.Y - c.StartPoint.Y; double x = c.EndPoint.X - c.StartPoint.X; double angle = Math.Atan(Math.Abs(y) / Math.Abs(x)); if (angle >= Math.PI / 2) { acBlkRef.TransformBy(Matrix3d.Rotation(0, curUCS.Zaxis, labelPoint)); } else { acBlkRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, labelPoint)); } acBlkRef.AddContext(occ.GetContext("10:1")); //Set value AttributeCollection attCol = acBlkRef.AttributeCollection; foreach (ObjectId attId in attCol) { AttributeReference att = acTrans.GetObject(attId, OpenMode.ForRead, false) as AttributeReference; if (att.Tag == "LEVEL") { att.UpgradeOpen(); att.TextString = p.FormationLevel.ToString(); } } BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); acTrans.AddNewlyCreatedDBObject(acBlkRef, true); } } acCurDb.Clayer = current; acTrans.Commit(); } }
public static ObjectId AddBlockRefToModelSpace(ObjectId blockTableRecordId, List <string> attrTextValues, Point3d location, Matrix3d matrix, Transaction trans, bool commit) { // Add a block reference to the model space BlockTableRecord ms = Tools.GetAcadBlockTableRecordModelSpace(trans, OpenMode.ForWrite); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(blockTableRecordId, OpenMode.ForRead); BlockReference br = new BlockReference(location, blockTableRecordId); br.TransformBy(matrix); ObjectContextManager ocm = btr.Database.ObjectContextManager; ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (btr.Annotative == AnnotativeStates.True) { br.AddContext(occ.CurrentContext); } //br.RecordGraphicsModified(true); ObjectId brId = ms.AppendEntity(br); trans.AddNewlyCreatedDBObject(br, true); // Add attributes from the block table record List <AttributeDefinition> attributes = GetAttributes(btr, trans); int i = 0; foreach (AttributeDefinition acAtt in attributes) { if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { //acAttRef.RecordGraphicsModified(true); acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform); //acAttRef.Position = acAtt.Position.TransformBy(br.BlockTransform); if (attrTextValues != null) { acAttRef.TextString = attrTextValues[i++]; } else { acAttRef.TextString = acAtt.TextString; } //if (acAtt.Annotative == AnnotativeStates.True) //acAttRef.AddContext(occ.CurrentContext); br.AttributeCollection.AppendAttribute(acAttRef); trans.AddNewlyCreatedDBObject(acAttRef, true); } } // Change the attribute definition to be displayed as backwards //acAtt.UpgradeOpen(); //acAtt.IsMirroredInX = true; //acAtt.IsMirroredInY = false; } br.RecordGraphicsModified(true); if (commit) { trans.Commit(); } return(brId); }
public BlockReference GetObject(Point3d position, List <string> attrTextValues) { if (this._blockId != ObjectId.Null) { _createBlockRecord(_name, _annotativeState, _origin, false); } BlockReference br = null; Tools.StartTransaction(() => { if (_blockRecordId != ObjectId.Null) { BlockTable bt = _db.BlockTableId.GetObjectForRead <BlockTable>(); BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObjectForWrite <BlockTableRecord>(); BlockTableRecord btr = _blockRecordId.GetObjectForRead <BlockTableRecord>(); br = new BlockReference(position, _blockRecordId); ObjectContextManager ocm = btr.Database.ObjectContextManager; ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (btr.Annotative == AnnotativeStates.True) { br.AddContext(occ.CurrentContext); } ObjectId brId = ms.AppendEntity(br); _db.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(br, true); // Add attributes from the block table record List <AttributeDefinition> attributes = btr.GetAttributes(); int i = 0; foreach (AttributeDefinition acAtt in attributes) { if (!acAtt.Constant) { using (AttributeReference acAttRef = new AttributeReference()) { //acAttRef.RecordGraphicsModified(true); acAttRef.SetAttributeFromBlock(acAtt, br.BlockTransform); //acAttRef.Position = acAtt.Position.TransformBy(br.BlockTransform); acAttRef.TextString = attrTextValues[i++]; //if (acAtt.Annotative == AnnotativeStates.True) //acAttRef.AddContext(occ.CurrentContext); br.AttributeCollection.AppendAttribute(acAttRef); _db.TransactionManager.TopTransaction.AddNewlyCreatedDBObject(acAttRef, true); } } } _insertPoint = position; _blockId = br.Id; } }); return(br); }
/// <summary> /// Вставка вхождения блока /// </summary> /// <param name="blName">Имя блока</param> /// <param name="pt">Точка вставки</param> /// <param name="owner">Контейнер</param> /// <param name="t"></param> /// <param name="scale"></param> /// <returns></returns> public static BlockReference InsertBlockRef(string blName, Point3d pt, BlockTableRecord owner, Transaction t, double scale = 1) { Database db = owner.Database; var bt = db.BlockTableId.GetObject( OpenMode.ForRead)as BlockTable; var btr = bt[blName].GetObject(OpenMode.ForRead) as BlockTableRecord; var blRef = new BlockReference(pt, btr.Id); blRef.Position = pt; if (blRef.Annotative == AnnotativeStates.True) { // Установка аннотативного масштаба blRef.AddContext(db.Cannoscale); } else if (scale != 1) { blRef.TransformBy(Matrix3d.Scaling(scale, pt)); } blRef.SetDatabaseDefaults(); owner.AppendEntity(blRef); t.AddNewlyCreatedDBObject(blRef, true); AddAttributes(blRef, btr, t); return blRef; }
static public void BlockJigCmd() { var doc = Application.DocumentManager.MdiActiveDocument; var db = doc.Database; var ed = doc.Editor; var pso = new PromptStringOptions("\nEnter block name: "); var pr = ed.GetString(pso); if (pr.Status != PromptStatus.OK) { return; } using (var tr = doc.TransactionManager.StartTransaction()) { var bt = (BlockTable)tr.GetObject( db.BlockTableId, OpenMode.ForRead ); if (!bt.Has(pr.StringResult)) { ed.WriteMessage( "\nBlock \"" + pr.StringResult + "\" not found."); return; } var ms = (BlockTableRecord)tr.GetObject( db.CurrentSpaceId, OpenMode.ForWrite ); var btr = (BlockTableRecord)tr.GetObject( bt[pr.StringResult], OpenMode.ForRead ); // Block needs to be inserted to current space before // being able to append attribute to it var br = new BlockReference(new Point3d(), btr.ObjectId); br.TransformBy(ed.CurrentUserCoordinateSystem); ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); if (btr.Annotative == AnnotativeStates.True) { var ocm = db.ObjectContextManager; var occ = ocm.GetContextCollection(annoScalesDict); br.AddContext(occ.CurrentContext); } else { br.ScaleFactors = new Scale3d(br.UnitFactor); } // Instantiate our map between attribute references // and their definitions var atts = new Dictionary <ObjectId, ObjectId>(); if (btr.HasAttributeDefinitions) { foreach (ObjectId id in btr) { var obj = tr.GetObject(id, OpenMode.ForRead); var ad = obj as AttributeDefinition; if (ad != null && !ad.Constant) { var ar = new AttributeReference(); // Set the initial positional information ar.SetAttributeFromBlock(ad, br.BlockTransform); ar.TextString = ad.TextString; // Add the attribute to the block reference // and transaction var arId = br.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); // Initialize our dictionary with the ObjectIds of // the attribute reference & definition atts.Add(arId, ad.ObjectId); } } } // Run the jig var jig = new BlockJig( ed.CurrentUserCoordinateSystem, tr, br, atts ); if (jig.Run() != PromptStatus.OK) { return; } // Commit changes if user accepted, otherwise discard tr.Commit(); } }
public static void testAttributedBlockInsert() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // change block name to your suit PromptEntityOptions pEnOpt = new PromptEntityOptions("\nPick Block Reference:"); pEnOpt.SetRejectMessage("\nObject Not Block Reference"); pEnOpt.AddAllowedClass(typeof(BlockReference), true); PromptEntityResult pEnRes = ed.GetEntity(pEnOpt); ObjectId EntId = pEnRes.ObjectId; //PromptResult pr = ed.GetString("\nType Block Name: "); //string blockName = pr.StringResult; string blockName = null; BlockReference UserBlockref = null; using (Transaction trans = db.TransactionManager.StartTransaction()) { UserBlockref = (BlockReference)trans.GetObject(EntId, OpenMode.ForRead); blockName = UserBlockref.Name; } Matrix3d ucs = ed.CurrentUserCoordinateSystem; //get current UCS matrix try { using (Transaction tr = db.TransactionManager.StartTransaction()) { // to force update drawing screen doc.TransactionManager.EnableGraphicsFlush(true); BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // if the block table doesn't already exists, exit if (!bt.Has(blockName)) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Block " + blockName + " does not exist."); return; } // insert the block in the current space PromptPointResult ppr = ed.GetPoint("\nSpecify insertion point: "); if (ppr.Status != PromptStatus.OK) { return; } DBObjectCollection dbobjcoll = ed.TraceBoundary(ppr.Value, false); Double area = 0; try { if (dbobjcoll.Count > 0) { BlockTableRecord blockTableRecmSpace = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead); ObjectIdCollection traceObjIds = new ObjectIdCollection(); foreach (DBObject obj in dbobjcoll) { Entity EntTrace = obj as Entity; if (EntTrace != null) { if (EntTrace is Polyline) { Polyline p = (Polyline)EntTrace; if (p.Closed) { area = p.Area; } } if (EntTrace is Line) { Line Ln = (Line)EntTrace; if (Ln.Closed) { area = Ln.Area; } } } } } } catch { throw; } string presisi = "#"; switch (db.Auprec) { case 0: presisi = "#"; break; case 1: presisi = "#0.0"; break; case 2: presisi = "#0.00"; break; case 3: presisi = "#0.000"; break; case 4: presisi = "#0.0000"; break; case 5: presisi = "#0.00000"; break; default: presisi = "#0.00"; break; } valueAreaBoundary = area.ToString(presisi); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES"); List<string> ListTag = new List<string>(); List<string> ListString = new List<string>(); Point3d pt = ppr.Value; BlockReference bref = new BlockReference(pt, bt[blockName]); Dictionary<string, string> dic = GetAttributDef(bref.BlockTableRecord, tr); foreach (KeyValuePair<string, string> item in dic) { ListTag.Add(item.Key.ToUpper().ToString()); // string[] info = item.Value.Split('|'); } formUserInputAttribut frmInput = new formUserInputAttribut(); IntPtr handle = AcAp.MainWindow.Handle; if (frmInput.ShowDialog(ListTag) == System.Windows.Forms.DialogResult.OK) { ListString.AddRange(frmInput.InputString); } else { return; } bref.Rotation = frmInput.UseRotation ? UserBlockref.Rotation : 0.0; bref.TransformBy(ucs); bref.AddContext(occ.CurrentContext); //add blockreference to current space btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); // set attributes to desired values ApplyAttibutes(db, tr, bref, ListTag, ListString); bref.RecordGraphicsModified(true); // to force updating a block reference tr.TransactionManager.QueueForGraphicsFlush(); tr.Commit(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message); } finally { // Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Pokey") } }
public void Generate() { Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database; ObjectContextCollection occ = acCurDb.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES"); Transaction acTrans = acCurDb.TransactionManager.TopTransaction; //Centre line Curve c = acTrans.GetObject(ObjectId, OpenMode.ForRead) as Curve; //Offset it LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead) as LayerTable; ObjectId current = acCurDb.Clayer; acCurDb.Clayer = acLyrTbl[Utilities.FoundationLayer]; // Open the Block table for read BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; DBObjectCollection offsets = c.GetOffsetCurves(FoundationWidth / 2); DBObjectCollection offsets2 = c.GetOffsetCurves(-(FoundationWidth / 2)); foreach (Entity e in offsets) { acBlkTblRec.AppendEntity(e); acTrans.AddNewlyCreatedDBObject(e, true); e.LayerId = acCurDb.Clayer; PositiveFoundationId = e.ObjectId; } foreach (Entity e in offsets2) { acBlkTblRec.AppendEntity(e); acTrans.AddNewlyCreatedDBObject(e, true); e.LayerId = acCurDb.Clayer; NegativeFoundationId = e.ObjectId; } acCurDb.Clayer = current; //Tag it acCurDb.Clayer = acLyrTbl[Utilities.FoundationTextLayer]; //Add foundation tag Matrix3d curUCSMatrix = Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem; CoordinateSystem3d curUCS = curUCSMatrix.CoordinateSystem3d; //Get lable point Point3d labelPoint3d = c.GetPointAtDist(c.GetDistanceAtParameter(c.EndParam) / 2); Point3d labelPoint = new Point3d(labelPoint3d.X, labelPoint3d.Y, 0); BlockTableRecord blockDef = acBlkTbl["FormationTag"].GetObject(OpenMode.ForRead) as BlockTableRecord; // Insert the block into the current space using (BlockReference acBlkRef = new BlockReference(labelPoint, acBlkTbl["FormationTag"])) { //Calculate Line Angle double y = c.EndPoint.Y - c.StartPoint.Y; double x = c.EndPoint.X - c.StartPoint.X; double angle = Math.Atan(Math.Abs(y) / Math.Abs(x)); if (angle >= Math.PI / 4) { acBlkRef.TransformBy(Matrix3d.Rotation(0, curUCS.Zaxis, labelPoint)); } else { acBlkRef.TransformBy(Matrix3d.Rotation(Math.PI / 2, curUCS.Zaxis, labelPoint)); } acBlkRef.AddContext(occ.GetContext("10:1")); acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acBlkTblRec.AppendEntity(acBlkRef); acTrans.AddNewlyCreatedDBObject(acBlkRef, true); // AttributeDefinitions foreach (ObjectId id in blockDef) { DBObject obj = id.GetObject(OpenMode.ForRead); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { //This is a non-constant AttributeDefinition //Create a new AttributeReference using (AttributeReference attRef = new AttributeReference()) { attRef.SetAttributeFromBlock(attDef, acBlkRef.BlockTransform); attRef.TextString = Parent.FormationLevel.ToString("F3"); //Add the AttributeReference to the BlockReference acBlkRef.AttributeCollection.AppendAttribute(attRef); acTrans.AddNewlyCreatedDBObject(attRef, true); } } } FormationTagId = acBlkRef.ObjectId; } acCurDb.Clayer = current; }