Hello
It is possible to tag window/ door in C#? I can import tag style from another drawing but how to attach to the window / door?
Solved! Go to Solution.
Solved by marcin.sachs. Go to Solution.
If you have your tag and it is already present in the drawing then it is simple to attach it to an object.
public bool Attach(Database db, MultiViewBlockReference tag, ObjectId doorId) { AnchorExtendedTagToEntity tagAnchor = new AnchorExtendedTagToEntity(); tagAnchor.SubSetDatabaseDefaults(db); tagAnchor.SetToStandard(db); tagAnchor.ForceHorizontal = false; tagAnchor.ReferencedEntityId = doorId; tag.SetAnchor(tagAnchor); }
hmm I'm doing something wrong...
this is my all code:
public class OpeningTagAndSchedule { public static void Create() { var acDoc = Application.DocumentManager.MdiActiveDocument; var ed = Application.DocumentManager.MdiActiveDocument.Editor; var acCurDb = acDoc.Database; var attachedObjects = new ObjectIdCollection(); using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { var bt = (BlockTable)acTrans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForWrite); var btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); try { TypedValue[] filterlist = new TypedValue[1]; //select doors and windows filterlist[0] = new TypedValue(8, "A-Window-G"); //Get objects // Request for objects to be selected in the drawing area SelectionFilter filter = new SelectionFilter(filterlist); PromptSelectionResult acSSPrompt = ed.GetSelection(filter); ObjectId[] ids = new ObjectId[0]; SelectionSet selectedobjects = SelectionSet.FromObjectIds(ids); if (acSSPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) { selectedobjects = acSSPrompt.Value; } // Get point PromptPointOptions usrPointOpts = new PromptPointOptions("Enter insertion point: "); //prompt for point PromptPointResult usrPointRes = ed.GetPoint(usrPointOpts); //get user point Point3d leftCornerPoint = usrPointRes.Value; //store user value Database dbDestination = HostApplicationServices.WorkingDatabase; string SourcePath = @"C:\\Program Files\\Autodesk\\ApplicationPlugins\\CAD+\\resources\\Tags & Schedules.dwg"; Database dbSource = new Database(false, true); dbSource.ReadDwgFile(SourcePath, System.IO.FileShare.Read, true, ""); //// get the Tags Style DictionaryMultiViewBlockDefinition TagStyle = new DictionaryMultiViewBlockDefinition(dbSource); ObjectIdCollection objCollectionsrc=new ObjectIdCollection(); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Window_Tag")); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Door_Tag")); CloningHelper helpme = new CloningHelper(); helpme.MergeType = DictionaryRecordMergeBehavior.Normal; helpme.Clone(dbSource, dbDestination, objCollectionSrc, TagStyle.RecordType, true); objCollectionSrc.Clear(); // get the Schedules Style DictionaryScheduleTableStyle ScheduleStyle = new DictionaryScheduleTableStyle(dbSource); //ObjectIdCollection objCollectionsrc=new ObjectIdCollection(); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Door Schedule")); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Window Schedule")); CloningHelper helpme2 = new CloningHelper(); helpme2.MergeType = DictionaryRecordMergeBehavior.Normal; helpme2.Clone(dbSource, dbDestination, objCollectionSrc, ScheduleStyle.RecordType, true); var tag = new MultiViewBlockReference(); tag.SetDatabaseDefaults(acCurDb); tag.SetToStandard(acCurDb); var mvBlks = new DictionaryMultiViewBlockDefinition(acCurDb); var mvBlkDefId = mvBlks.GetAt("MiTek_Window_Tag"); tag.StyleId = mvBlkDefId; tag.Scale = new Scale3d(1); btr.AppendEntity(tag); acTrans.AddNewlyCreatedDBObject(tag, true); //MultiViewBlockReference tag = "MiTek_Window_Tag" foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId obj in selectedobjects) { //Attach(acCurDb, tag, obj); } Vector3d vector1 = new Vector3d(3500, 0, 0); Point3d leftCornerPoint2 = new Point3d(); leftCornerPoint2 = leftCornerPoint.Add(vector1); ed.Command("-SCHEDULEADD", "MiTek Door Schedule", "A-Door-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint, ""); ed.Command("-SCHEDULEADD", "MiTek Window Schedule", "A-Window-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint2, ""); } catch (System.Exception e) { ed.WriteMessage(e.Message); } acTrans.Commit(); } } public static void Attach(Database db, MultiViewBlockReference tag, ObjectId objId) { AnchorExtendedTagToEntity tagAnchor = new AnchorExtendedTagToEntity(); tagAnchor.SubSetDatabaseDefaults(db); tagAnchor.SetToStandard(db); tagAnchor.ForceHorizontal = false; tagAnchor.ReferencedEntityId = objId; tag.SetAnchor(tagAnchor); } }
What line is causing the error in visual studio?
I also noticed that you created a new mvblock reference and added it to the database but I do not see where you are setting its location. It might default to 0,0,0 but i am not sure.
error is obtained in AutoCAD not in Visual Studio
only if I use:
foreach (Autodesk.AutoCAD.DatabaseServices.ObjectId obj in selectedobjects) { }
Sorry i didnt catch that error. A SelectionSet is a container for a SelectedObject not an ObjectId.
You need to do this instead.
foreach (SelectedObject obj in selectedObjects) { // do something with the obj.ObjectId }
Sorry for my previous post I'm writing this quickly on my phone.
I create LineSegment2d from start point to end point of opening. I'm getting mid point of linesegment2d I'm trying insert tag in this point but it is inserted always in start point of opening.
public static void Create() { var acDoc = Application.DocumentManager.MdiActiveDocument; var ed = Application.DocumentManager.MdiActiveDocument.Editor; var acCurDb = acDoc.Database; var attachedObjects = new ObjectIdCollection(); using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { var bt = (BlockTable)acTrans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForWrite); var btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); try { TypedValue[] filterlist = new TypedValue[1]; //select doors and windows filterlist[0] = new TypedValue(8, "A-Door-G,A-Window-G"); //Get objects // Request for objects to be selected in the drawing area SelectionFilter filter = new SelectionFilter(filterlist); PromptSelectionResult acSSPrompt = ed.GetSelection(filter); ObjectId[] ids = new ObjectId[0]; SelectionSet selectedobjects = SelectionSet.FromObjectIds(ids); if (acSSPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) { selectedobjects = acSSPrompt.Value; } // Get point PromptPointOptions usrPointOpts = new PromptPointOptions("Enter insertion point: "); //prompt for point PromptPointResult usrPointRes = ed.GetPoint(usrPointOpts); //get user point Point3d leftCornerPoint = usrPointRes.Value; //store user value Database dbDestination = HostApplicationServices.WorkingDatabase; string SourcePath = @"C:\\Program Files\\Autodesk\\ApplicationPlugins\\CAD+\\resources\\Tags & Schedules.dwg"; Database dbSource = new Database(false, true); dbSource.ReadDwgFile(SourcePath, System.IO.FileShare.Read, true, ""); //// get the Tags Style DictionaryMultiViewBlockDefinition TagStyle = new DictionaryMultiViewBlockDefinition(dbSource); ObjectIdCollection objCollectionsrc=new ObjectIdCollection(); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Window_Tag")); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Door_Tag")); CloningHelper helpme = new CloningHelper(); helpme.MergeType = DictionaryRecordMergeBehavior.Normal; helpme.Clone(dbSource, dbDestination, objCollectionSrc, TagStyle.RecordType, true); objCollectionSrc.Clear(); // get the Schedules Style DictionaryScheduleTableStyle ScheduleStyle = new DictionaryScheduleTableStyle(dbSource); //ObjectIdCollection objCollectionsrc=new ObjectIdCollection(); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Door Schedule")); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Window Schedule")); CloningHelper helpme2 = new CloningHelper(); helpme2.MergeType = DictionaryRecordMergeBehavior.Normal; helpme2.Clone(dbSource, dbDestination, objCollectionSrc, ScheduleStyle.RecordType, true); MiiInsulation.Common.GetLayer(acTrans, acCurDb, "A-Door-T", (short)21, false); MiiInsulation.Common.GetLayer(acTrans, acCurDb, "A-Window-T", (short)91, false); foreach (SelectedObject obj in selectedobjects) { Autodesk.Aec.DatabaseServices.DictionaryMultiViewBlockDefinition mvbstyledict; ObjectId bstyleID = obj.ObjectId; var geo = bstyleID.GetObject(OpenMode.ForRead, false) as Geo; Autodesk.Aec.DatabaseServices.MultiViewBlockReference tag; tag = new Autodesk.Aec.DatabaseServices.MultiViewBlockReference(); Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = acCurDb.TransactionManager; mvbstyledict = new Autodesk.Aec.DatabaseServices.DictionaryMultiViewBlockDefinition(acCurDb); LineSegment2d line = new LineSegment2d(new Point2d(geo.StartPoint.X , geo.StartPoint.Y), new Point2d(geo.StartPoint.X, geo.StartPoint.Y).GetVectorTo(new Point2d(geo.EndPoint.X, geo.EndPoint.Y))); Point3d midpoint = new Point3d(line.MidPoint.X, line.MidPoint.Y, 0.0); string layer = ""; Autodesk.AutoCAD.DatabaseServices.DBObject objk = acTrans.GetObject(bstyleID, OpenMode.ForRead); Autodesk.Aec.Arch.DatabaseServices.Door door = objk as Autodesk.Aec.Arch.DatabaseServices.Door; Autodesk.Aec.Arch.DatabaseServices.Window window = objk as Autodesk.Aec.Arch.DatabaseServices.Window; if (door != null) { bstyleID = mvbstyledict.GetAt("MiTek_Door_Tag"); layer = "A-Door-T"; ed.WriteMessage("\nDoor"); } if (window != null) { bstyleID = mvbstyledict.GetAt("MiTek_Window_Tag"); layer = "A-Window-T"; ed.WriteMessage("\nWindow"); } objk.Dispose(); tag.BlockDefId = bstyleID; tag.Layer = layer; tag.Scale = new Scale3d(100); //tag.Direction = geo.Direction; tag.Location = midpoint; btr.AppendEntity(tag); acTrans.AddNewlyCreatedDBObject(tag, true); Attach(acCurDb, tag, obj.ObjectId); } Vector3d vector1 = new Vector3d(3500, 0, 0); Point3d leftCornerPoint2 = new Point3d(); leftCornerPoint2 = leftCornerPoint.Add(vector1); ed.Command("-SCHEDULEADD", "MiTek Door Schedule", "A-Door-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint, ""); ed.Command("-SCHEDULEADD", "MiTek Window Schedule", "A-Window-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint2, ""); } catch (System.Exception e) { ed.WriteMessage(e.Message); } acTrans.Commit(); } } public static void Attach(Database db, MultiViewBlockReference tag, ObjectId objId) { AnchorExtendedTagToEntity tagAnchor = new AnchorExtendedTagToEntity(); tagAnchor.SubSetDatabaseDefaults(db); tagAnchor.SetToStandard(db); tagAnchor.ForceHorizontal = false; tagAnchor.ReferencedEntityId = objId; tag.SetAnchor(tagAnchor); tag.Location = new Point3d(0,0,0); }
It's working!
full working code below:
public class OpeningTagAndSchedule { public static void OpeningTagging() { var acDoc = Application.DocumentManager.MdiActiveDocument; var ed = Application.DocumentManager.MdiActiveDocument.Editor; var acCurDb = acDoc.Database; var attachedObjects = new ObjectIdCollection(); using (var acTrans = acCurDb.TransactionManager.StartTransaction()) { var bt = (BlockTable)acTrans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForWrite); var btr = (BlockTableRecord)acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); try { TypedValue[] filterlist = new TypedValue[1]; //select doors and windows filterlist[0] = new TypedValue(8, "A-Door-G,A-Window-G"); //Get objects // Request for objects to be selected in the drawing area SelectionFilter filter = new SelectionFilter(filterlist); PromptSelectionResult acSSPrompt = ed.GetSelection(filter); ObjectId[] ids = new ObjectId[0]; SelectionSet selectedobjects = SelectionSet.FromObjectIds(ids); if (acSSPrompt.Status == Autodesk.AutoCAD.EditorInput.PromptStatus.OK) { selectedobjects = acSSPrompt.Value; } // Get point PromptPointOptions usrPointOpts = new PromptPointOptions("Enter insertion point: "); //prompt for point PromptPointResult usrPointRes = ed.GetPoint(usrPointOpts); //get user point Point3d leftCornerPoint = usrPointRes.Value; //store user value Database dbDestination = HostApplicationServices.WorkingDatabase; string SourcePath = @"C:\\Program Files\\Autodesk\\ApplicationPlugins\\CAD+\\resources\\Tags & Schedules.dwg"; Database dbSource = new Database(false, true); dbSource.ReadDwgFile(SourcePath, System.IO.FileShare.Read, true, ""); //// get the Tags Style DictionaryMultiViewBlockDefinition TagStyle = new DictionaryMultiViewBlockDefinition(dbSource); ObjectIdCollection objCollectionsrc=new ObjectIdCollection(); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Window_Tag")); objCollectionSrc.Add(TagStyle.GetAt("MiTek_Door_Tag")); CloningHelper helpme = new CloningHelper(); helpme.MergeType = DictionaryRecordMergeBehavior.Normal; helpme.Clone(dbSource, dbDestination, objCollectionSrc, TagStyle.RecordType, true); objCollectionSrc.Clear(); // get the Schedules Style DictionaryScheduleTableStyle ScheduleStyle = new DictionaryScheduleTableStyle(dbSource); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Door Schedule")); objCollectionSrc.Add(ScheduleStyle.GetAt("MiTek Window Schedule")); CloningHelper helpme2 = new CloningHelper(); helpme2.MergeType = DictionaryRecordMergeBehavior.Normal; helpme2.Clone(dbSource, dbDestination, objCollectionSrc, ScheduleStyle.RecordType, true); dbSource.CloseInput(true); CAD_.Functions.BasicFunctions.CreateLayer("A-Window-T", Color.FromColorIndex(ColorMethod.ByAci, 91), "Fine"); CAD_.Functions.BasicFunctions.CreateLayer("A-Door-T", Color.FromColorIndex(ColorMethod.ByAci, 21), "Fine"); foreach (SelectedObject obj in selectedobjects) { Autodesk.Aec.DatabaseServices.DictionaryMultiViewBlockDefinition mvbstyledict; ObjectId bstyleID = obj.ObjectId; var geo = bstyleID.GetObject(OpenMode.ForRead, false) as Geo; Autodesk.Aec.DatabaseServices.MultiViewBlockReference tag; tag = new Autodesk.Aec.DatabaseServices.MultiViewBlockReference(); mvbstyledict = new Autodesk.Aec.DatabaseServices.DictionaryMultiViewBlockDefinition(acCurDb); LineSegment2d line = new LineSegment2d(new Point2d(geo.StartPoint.X , geo.StartPoint.Y), new Point2d(geo.StartPoint.X, geo.StartPoint.Y).GetVectorTo(new Point2d(geo.EndPoint.X, geo.EndPoint.Y))); Point3d midpoint = new Point3d(line.MidPoint.X, line.MidPoint.Y, 0.0); string layer = ""; Autodesk.AutoCAD.DatabaseServices.DBObject objk = acTrans.GetObject(bstyleID, OpenMode.ForRead); Autodesk.Aec.Arch.DatabaseServices.Door door = objk as Autodesk.Aec.Arch.DatabaseServices.Door; Autodesk.Aec.Arch.DatabaseServices.Window window = objk as Autodesk.Aec.Arch.DatabaseServices.Window; if (door != null) { bstyleID = mvbstyledict.GetAt("MiTek_Door_Tag"); layer = "A-Door-T"; } if (window != null) { bstyleID = mvbstyledict.GetAt("MiTek_Window_Tag"); layer = "A-Window-T"; } objk.Dispose(); double length = geo.StartPoint.DistanceTo(geo.EndPoint) / 2; tag.BlockDefId = bstyleID; tag.Layer = layer; tag.Scale = new Scale3d(100); tag.Location = new Point3d(tag.Location.X + length, tag.Location.Y + 400, tag.Location.Z); tag.Rotation = 0; btr.AppendEntity(tag); acTrans.AddNewlyCreatedDBObject(tag, true); Attach(acCurDb, tag, obj.ObjectId, midpoint); } Vector3d vector1 = new Vector3d(3500, 0, 0); Point3d leftCornerPoint2 = new Point3d(); leftCornerPoint2 = leftCornerPoint.Add(vector1); ed.Command("-SCHEDULEADD", "MiTek Door Schedule", "A-Door-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint, ""); ed.Command("-SCHEDULEADD", "MiTek Window Schedule", "A-Window-G", "_N", "_N", "_Y", "_Y", selectedobjects, "", leftCornerPoint2, ""); } catch (System.Exception e) { ed.WriteMessage(e.Message); } acTrans.Commit(); } } public static void Attach(Database db, MultiViewBlockReference tag, ObjectId objId, Point3d midpoint) { AnchorExtendedTagToEntity tagAnchor = new AnchorExtendedTagToEntity(); tagAnchor.SubSetDatabaseDefaults(db); tagAnchor.SetToStandard(db); tagAnchor.ForceHorizontal = true; tagAnchor.ReferencedEntityId = objId; tag.SetAnchor(tagAnchor); } }