.NET

Reply
Active Contributor
xpeter88
Posts: 42
Registered: ‎11-14-2013
Message 1 of 4 (344 Views)
Accepted Solution

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

344 Views, 3 Replies
02-02-2014 08:58 AM

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

 

Mentor
SENL1362
Posts: 150
Registered: ‎07-20-2011
Message 2 of 4 (318 Views)

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

02-02-2014 11:59 AM 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;
...
Mentor
mzakiralam
Posts: 225
Registered: ‎11-09-2012
Message 3 of 4 (317 Views)

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

02-02-2014 12:01 PM 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...

Active Contributor
xpeter88
Posts: 42
Registered: ‎11-14-2013
Message 4 of 4 (276 Views)

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

02-03-2014 02:39 AM 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

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community