Issue
I want to anchor a tag to an entity. I used AnchorTagToEntity. But it behaves differently. I want to make the tag behaves like a door tag added by the UI. How can I do that?
Solution
ACA uses AnchorExtendedTagToEntity to anchor a tag. It has extra functionality to the AnchorTagToEntity, for example, a tag moves together when the anchored object (e.g., a door) moves, keeping the same distance. The below is a sample code that demonstrates how to add the anchor. For simplicity, I'm assuming that the tag object is in the drawing already.
' Anchor a tag to an entity.
<CommandMethod( _
"ACANetScheduleLabs", "AcaAnchorTag", CommandFlags.Modal)> _
Public Sub anchorExtendedTagToEnt()
Dim doc As Document =
Application.DocumentManager.MdiActiveDocument
Dim ed As Editor = doc.Editor
Dim db As Database = doc.Database
' (1) select a Geo
' Geo is a base class for AEC objects.
Dim optGeo As New PromptEntityOptions(
"Select a geo to anchor a tag")
optGeo.SetRejectMessage("Not a geo")
optGeo.AddAllowedClass(GetType(Geo), False)
Dim resGeo As PromptEntityResult = ed.GetEntity(optGeo)
If resGeo.Status <> PromptStatus.OK Then
ed.WriteMessage("failed to pick an geo")
Exit Sub
End If
Dim idGeo As ObjectId = resGeo.ObjectId
' (2) Select a tag
Dim optTag As New PromptEntityOptions("Select a tag ")
optTag.SetRejectMessage("Not a tag")
optTag.AddAllowedClass(GetType(MultiViewBlockReference), True)
Dim resTag As PromptEntityResult = ed.GetEntity(optTag)
If resTag.Status <> PromptStatus.OK Then
ed.WriteMessage("failed to pick a tag")
Exit Sub
End If
Dim idTag As ObjectId = resTag.ObjectId
' (3) Pick a location to place the tag
Dim resPt As PromptPointResult =
ed.GetPoint("Pick a location of the tag ")
If resPt.Status <> PromptStatus.OK Then
ed.WriteMessage("failed to pick a location of the tag")
Exit Sub
End If
' (4) Add a tag to the geo using AnchorExtendedTagToEntity
Dim trans As Transaction =
db.TransactionManager.StartTransaction()
Try
' Open the geo for read
Dim obj As Object = trans.GetObject(idGeo, OpenMode.ForRead)
Dim objGeo As Geo = CType(obj, Geo)
Dim ecsEnt As Matrix3d = objGeo.Ecs()
' Oopen the tag for write
obj = trans.GetObject(idTag, OpenMode.ForWrite)
Dim objTag As MultiViewBlockReference =
CType(obj, MultiViewBlockReference)
' Adjust the location of the tag
Dim pt As Point3d = resPt.Value
objTag.Location = pt.TransformBy(ecsEnt.Inverse())
' Add an anchor between the tag and the geo
Dim pAnchor As New AnchorExtendedTagToEntity
pAnchor.SubSetDatabaseDefaults(db)
pAnchor.SetToStandard(db)
pAnchor.ForceHorizontal = False '' or True
' Here is the main part that attaches the tag to the geo
' entity (e.g., door and window)
pAnchor.ReferencedEntityId = idGeo
objTag.SetAnchor(pAnchor)
trans.Commit()
Catch ex As Exception
trans.Abort()
Finally
trans.Dispose()
End Try
End Sub