How to Get AttributeReference object using Editor.GetSelection

How to Get AttributeReference object using Editor.GetSelection

Anonymous
Not applicable
899 Views
2 Replies
Message 1 of 3

How to Get AttributeReference object using Editor.GetSelection

Anonymous
Not applicable

I'm trying to use Editor.GetSelection to allow my end users to select "text" in their drawing that will be used to build a list in my front end.  I'd like for the user to simply select via window or crossing (or by holding ctrl to select attributes) any number of objects in their drawing from a single prompt.  I have set AllowSubSelections to true on my PromptSelectionOptions.  However I can't seem to obtain the ObjectID of a selected Attribute - I seem to only be able to get at the BlockReference object of the selected attribute.  Here is my current code of my last attempt.  Can somebody can help me get to the attributereference object?

 

If TypeOf acEntity Is Autodesk.AutoCAD.DatabaseServices.BlockReference Then
     Dim acSelectedSubObjectArray As Autodesk.AutoCAD.EditorInput.SelectedSubObject()
     acSelectedSubObjectArray = acSelectedObject.GetSubentities()
     Dim acSelectedSubObject As Autodesk.AutoCAD.EditorInput.SelectedSubObject
     For Each acSelectedSubObject In acSelectedSubObjectArray
          Dim acObjectIDsArray As Autodesk.AutoCAD.DatabaseServices.ObjectId()
          acObjectIDsArray = acSelectedSubObject.FullSubentityPath.GetObjectIds()
         Dim acObjectID As Autodesk.AutoCAD.DatabaseServices.ObjectId
         For Each acObjectID In acObjectIDsArray
              If TypeOf acTrans.GetObject(acObjectID, OpenMode.ForRead) Is Autodesk.AutoCAD.DatabaseServices.AttributeReference Then
                     Dim acAttributeReference As Autodesk.AutoCAD.DatabaseServices.AttributeReference
                    acAttributeReference = acTrans.GetObject(acObjectID, OpenMode.ForRead)
                    AddTextReplaceListItem(acAttributeReference.TextString, "")
             End If
         Next
     Next
End If

0 Likes
900 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

Attribute reference are not what AutoCAD API calls 'SubObject' (e.g. the faces or edges of Solid 3d are 'SubObject').

 

If you want to get attributes references by selection, you have to select BlockReference instances an iterate through their AttributeCollection.

 

Here's an example using a selection filter to select only block references with attributes.

 

        [CommandMethod("CMD")]
        public void Cmd()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

// build a selection filter var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT"), // block reference new TypedValue(66, 1) // with attributes });
// Get a filtered selection from the user var selection = ed.GetSelection(filter); if (selection.Status != PromptStatus.OK) return; using (var tr = db.TransactionManager.StartTransaction()) {
// Iterate through the selection which only contains attributed blocks foreach (SelectedObject selectedObj in selection.Value) { var blockRef = (BlockReference)tr.GetObject(selectedObj.ObjectId, OpenMode.ForRead);

// Iterate through the block attribute collection foreach (ObjectId id in blockRef.AttributeCollection) { var attRef = (AttributeReference)tr.GetObject(id, OpenMode.ForRead); ed.WriteMessage($"\n{attRef.TextString}"); } } tr.Commit(); } }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

norman.yuan
Mentor
Mentor

Rather than getting a SelectionSet by calling Editor.GetSelection, if it is OK to ask user to pick attribute in a block (BlockReference) one by one in your process, then you can use PromptNestedEntityOprion in conjunction with Editor.GetNestedEntity(), and then in the returned PromptResult, verify that the ObjectId returned is an AttributeReference. Something like:

 

var opt=new PromptNestedEntityOpt("\nPick an anntribute:");

var res=ed.getNestedEntity(opt);

if (res.Status==PromptStatus.OK)

{

  if (res.ObjectId.ObjectClass.DxfName.ToUpper()=="ATTRIB")

  {

    //User picked an attribute in the block. Do things as expected

  }

}

Norman Yuan

Drive CAD With Code

EESignature

0 Likes