.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VB.NET Select attribute definitions (attdef) and change their values

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
xpeter88
2367 Views, 3 Replies

VB.NET Select attribute definitions (attdef) and change their values

Hi,

I would like to change attribute values but seems that I cannot access them and don't know why.

I was trying to select all attribute definitions in current drawing - successfuly - and then tried to change their values based on it's attribute tags.

 

It turned out that I cannot access them from the selection set. How can I do it then? Attribute definitions are not nested in blocks, just definitions in the drawings.

 

  Sub ChangeAttDefs()
            Using acDoc.LockDocument()
                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                    ' Create a TypedValue array to define the filter criteria
                    Dim acTypValAr(0) As TypedValue
                    acTypValAr.SetValue(New TypedValue(DxfCode.Start, "ATTDEF"), 0)
                    '' Assign the filter criteria to a SelectionFilter object
                    Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)
                    '' Request for objects to be selected in the drawing area
                    Dim acSSPrompt As PromptSelectionResult = acDoc.Editor.SelectAll(acSelFtr)
                    If acSSPrompt.Status = PromptStatus.OK Then
                        Dim acSSet As SelectionSet = acSSPrompt.Value
                        '' Step through the objects in the selection set
                        For Each acSSObj As SelectedObject In acSSet
                            '' Open the selected object for write
                            Dim acAttRef As AttributeReference = acTrans.GetObject(acSSObj.ObjectId, OpenMode.ForWrite)
                            '' Change attribute values
                            If acAttRef.Tag = "TEST" Then
                                acAttRef.TextString = "WORKED"
                                Exit Sub
                            End If
                        Next
                        acTrans.Commit()
                    End If
                End Using
            End Using

        End Sub

 

3 REPLIES 3
Message 2 of 4
SENL1362
in reply to: xpeter88

you'll find attributereferences coupled to blockreferences and attributedefinitions coupled to blockdefinitions:

PromptEntityOptions peo = new PromptEntityOptions("\nSelect a block reference:");
peo.SetRejectMessage("Block References only");
peo.AddAllowedClass(typeof(BlockReference), true);
PromptEntityResult per = ed.GetEntity(peo);
BlockReference br = (BlockReference)tr.GetObject(per.ObjectId,OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(br.BlockTableRecord,OpenMode.ForRead);
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId id in btr)
{
DBObject obj = tr.GetObject(id, OpenMode.ForRead);
if (obj is AttributeDefinition)
{
AttributeDefinition attDef = (AttributeDefinition)obj;
...
Message 3 of 4
mzakiralam
in reply to: xpeter88

Hi ,

your code is not working because you did not open the blocktablerecord. To change the attribute definition , you should open the blocktablerecord first then you can edit it. You have only open the block and try to find the attribute definition. This is why it is not getting attef and not doing anything. So, first open the block table record from your selection set. Then try to change attdef. you can have a look on below link which may be useful for you.

 

http://through-the-interface.typepad.com/through_the_interface/2010/07/swapping-autocad-block-attrib...

Message 4 of 4
xpeter88
in reply to: mzakiralam

"So, first open the block table record from your selection set. Then try to change attdef." that is it. Now it works fine. Many thanks,

 

Peter

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost