Problem with Inserting a Tag/Multi-View Block Reference

Problem with Inserting a Tag/Multi-View Block Reference

michaeleGFMZG
Contributor Contributor
594 Views
3 Replies
Message 1 of 4

Problem with Inserting a Tag/Multi-View Block Reference

michaeleGFMZG
Contributor
Contributor

Hello everyone

 

So I wrote some code to insert a TAG into a drawing for each MEP Wire that is found in model space. The code works fine except for one problem: The tag is inserted as a Multi View Block Reference instead of a Tag. Tags are created from multi-view block definitions but I can't seem to figure out where I'm going wrong. 

 

I've provided pictures of the native Tag (Created using the native AutoCAD Tag creation tool) and my custom tag created from the vb.net code.

 

Both have the same information in the properties palette, but AutoCAD reads my custom tag as a "multi view block" rather than as a tag like the native tag. You can see this in the top of the ribbon with the additional options available for a native Tag. Additionally, my custom tag has no grips nor the option to edit the attributes. 

 

My code uses the native tag's definition to create a new multiview block reference which attaches to all the Wires in blue. I'll include my code below. Thanks for anyone that can help.

 

[
Dim doc As Document = application.DocumentManager.MdiActiveDocument
Dim docdb As Database = doc.Database
Dim ED As Editor = doc.Editor


<CommandMethod("AddTag2Wire")>
Public Sub AddTag2Wire()

Dim PEO As New PromptEntityOptions(vbNewLine + "Select The Tag To Replicate")

Dim Prompt = ED.GetEntity(PEO)
Dim TagObjectId = Prompt.ObjectId

If Prompt.Status = PromptStatus.Cancel Then
Return
End If


Dim PEO2 As New PromptIntegerOptions(vbNewLine + "Input Text Height (4, 6 or 8)")

Dim PromptintegerResult = ED.GetInteger(PEO2)

If PromptIntegerResult.Status = PromptStatus.Cancel Then
Return
End If

Dim PIR = PromptintegerResult.Value

Using trans As Transaction = docdb.TransactionManager.StartTransaction

Dim BLOCKTABLE As BlockTable = trans.GetObject(docdb.BlockTableId, OpenMode.ForRead)
Dim MODELSPACE As BlockTableRecord = trans.GetObject(BLOCKTABLE(BlockTableRecord.ModelSpace), OpenMode.ForWrite) 'OPENS MODEL SPACE
Dim OpenTag As MultiViewBlockReference = trans.GetObject(TagObjectId, OpenMode.ForRead)
Dim OpenTagDefinitionId = OpenTag.BlockDefId

For Each ObjectId As objectid In MODELSPACE
If ObjectId.ObjectClass.Name = "AecbDbWire" Then
Dim OpenWire As Wire = trans.GetObject(ObjectId, OpenMode.ForRead)
Dim Location = OpenWire.Location
Dim InsertLocation As New Point3d(Location.X, Location.Y, 0)

Dim NewTag As New MultiViewBlockReference
NewTag.BlockDefId = OpenTagDefinitionId

If PIR = 4 Then
Dim TagScale As New Scale3d(4, 4, 1)
NewTag.Scale = TagScale
ElseIf PIR = 6 Then
Dim TagScale As New Scale3d(6, 6, 1)
NewTag.Scale = TagScale
ElseIf PIR = 8 Then
Dim TagScale As New Scale3d(8, 8, 1)
NewTag.Scale = TagScale
Else
ED.WriteMessage(vbNewLine + "Only 4, 6 or 8 Are Acceptable Arguments")
Return
End If

MODELSPACE.AppendEntity(NewTag)
trans.AddNewlyCreatedDBObject(NewTag, True)

 

Dim TagAnchor As New AnchorExtendedTagToEntity
TagAnchor.SubSetDatabaseDefaults(docdb)
TagAnchor.SetToStandard(docdb)
TagAnchor.ForceHorizontal = True
TagAnchor.ReferencedEntityId = ObjectId
NewTag.SetAnchor(TagAnchor)
NewTag.Location = New Point3d(0, 0, 0)

Else Continue For
End If
Next

trans.Commit()
End Using

]

 

 

0 Likes
Accepted solutions (1)
595 Views
3 Replies
Replies (3)
Message 2 of 4

michaeleGFMZG
Contributor
Contributor

So, I ended up figuring out a solution.

 

Once the program runs, I have to change the multi-view block's definition in the properties palette, and then change it back to the original definition. This seems to "refresh" the multi view block in some way and makes the grips available. However, when selected, its not defined as a Tag, but still functions as one.

 

I'd prefer it to be recognized as a tag, but as long as the behavior is what I need, I won't complain. Any fixes or suggestions still welcome.

0 Likes
Message 3 of 4

Gepaha
Collaborator
Collaborator
Accepted solution

For the "Tag" menu to appear when the MultiViewBlockReference Tag is selected, the information must be attached via XData.
An easy way to do this is to copy the XData data from the MultiviewBlockReference Tag used.

  newTag.XData = OpenTag.XData
        [CommandMethod("AddTag2Wire", CommandFlags.Modal)]
        public void AddTag2Wire()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;           

            PromptEntityOptions peo = new PromptEntityOptions("\nSelect MultiviewBlockreference tag To Replicate ");
            peo.SetRejectMessage("\nNot a MultiViewBlockReference tag.");
            peo.AddAllowedClass(typeof(MultiViewBlockReference), false);
            PromptEntityResult per = ed.GetEntity(peo);

            if (per.Status != PromptStatus.OK)
            {
                ed.WriteMessage("\nSelection error - aborting");
                return;
            }
            
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                MultiViewBlockReference OpenTag = trans.GetObject(per.ObjectId, OpenMode.ForRead) as MultiViewBlockReference;
                AcDb.ObjectId OpenTagDefinitionId = OpenTag.BlockDefId;

                foreach (AcDb.ObjectId objectId in ms)
                {
                    if ((objectId.ObjectClass.Name == "AecbDbWire"))
                    {
                        Wire OpenWire = trans.GetObject(objectId, OpenMode.ForRead) as Wire;
                        Point3d location = OpenWire.Location;
                        Point3d InsertLocation = new Point3d(location.X, location.Y, 0);
                        MultiViewBlockReference newTag = new MultiViewBlockReference();
                        newTag.BlockDefId = OpenTagDefinitionId;
                        newTag.Scale = OpenTag.Scale;
                        newTag.Layer = OpenTag.Layer;
                        newTag.XData = OpenTag.XData;

                        ms.AppendEntity(newTag);
                        trans.AddNewlyCreatedDBObject(newTag, true);
                        AnchorExtendedTagToEntity tagAnchor = new AnchorExtendedTagToEntity();
                        tagAnchor.SubSetDatabaseDefaults(db);
                        tagAnchor.SetToStandard(db);
                        tagAnchor.ForceHorizontal = true;
                        tagAnchor.ReferencedEntityId = objectId;
                        tagAnchor.AllowIndependentUpdate = true;
                        newTag.SetAnchor(tagAnchor);
                        newTag.Location = location;
                    }            
                }
                trans.Commit();
            }
        }         
    }

 

gphanauer_0-1708365800722.png

 

gphanauer_1-1708365862781.png

 

0 Likes
Message 4 of 4

michaeleGFMZG
Contributor
Contributor

@Gepaha 

Thank you for the help! 

I'll work on updating this over the weekend. Seems quite easy to incorporate XData... I'll have to read into XData and how else it can be applied.

 

Looks like you're using AutoCAD MEP also! It's nice to see someone else using this vertical. I only have about 4 months experience customizing MEP, but if you want to exchange some ideas, feel free to message me directly.

 

0 Likes