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

Edit attribute VB.NET

28 REPLIES 28
Reply
Message 1 of 29
scarta
7863 Views, 28 Replies

Edit attribute VB.NET

Hi..
Can anyone tell me where I can find a VB.NET sample for editing attribute value on an inserted block in a drawing?
I have found the sample for read attribute, but not for write.

Thank

Stefano
28 REPLIES 28
Message 2 of 29
Anonymous
in reply to: scarta

Have you tried searching the newsgroup, and looking at the online docs at
the URL below?

http://www.autodesk.com/autocad-net-developers-guide


--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message
news:6297143@discussion.autodesk.com...
Hi..
Can anyone tell me where I can find a VB.NET sample for editing attribute
value on an inserted block in a drawing?
I have found the sample for read attribute, but not for write.

Thank

Stefano
Message 3 of 29
scarta
in reply to: scarta

Hi,
Thank for replay
I have searched on forum, but nothing.

I have a titleblock in a drawing and I have to modify the attribute's value. I have to find a specific TAG and change the value.
Can you help me?
Message 4 of 29
_gile
in reply to: scarta

Hi,

Here's a little C# sample.
Assuming:
myBlockRef is the block reference
myTag is the tag string
newValue is the attribute new value (string)

{code}AttributeCollection attCol = myBlockRef.AttributeCollection;
foreach (ObjectId attId in attCol)
{
AttributeReference att = tr.GetObject(attId, OpenMode.ForRead, false) as AttributeReference;
if (att.Tag == myTag)
{
att.UpgradeOpen();
att.TextString = newValue;
}
}{code}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 29
Anonymous
in reply to: scarta

62 hits for attributereference in this forum. That's a lot of reading.
Message 6 of 29
scarta
in reply to: scarta

I have searched in forum but I don't find a sample for change attribute value of a block in drawing
this is my code:
I find the tag and change value, but the block in drawing don't change value

myDB = ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction

myDwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR = myBT(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForWrite)


strNomeBlocco = "ATTRIBUTI"
For y = 1 To myBlocks.Count
myBlockIDs = PopulateOIDCs(myBlocks(y - 1))
For J = 1 To myBlockIDs.Count
myBlockRef = myBlockIDs(J - 1).GetObject(DatabaseServices.OpenMode.ForWrite)
If UCase(myBlockRef.Name) = strNomeBlocco Then



Dim attCol As AttributeCollection = myBlockRef.AttributeCollection

For Each attId As ObjectId In attCol
Dim att As AttributeReference = TryCast(myTrans.GetObject(attId, OpenMode.ForWrite, False), AttributeReference)
If att.Tag = "TITOLO_RIGA_1" Then
att.UpgradeOpen()
att.TextString = "prova"
att.Dispose()
End If
Next


End If

Next
Next

myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
Message 7 of 29
Anonymous
in reply to: scarta

Are we supposed to guess what your PopulateOIDCs() method does?

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message
news:6298025@discussion.autodesk.com...
I have searched in forum but I don't find a sample for change attribute
value of a block in drawing
this is my code:
I find the tag and change value, but the block in drawing don't change value

myDB =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction

myDwg =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
myBT = myDwg.Database.BlockTableId.GetObject(OpenMode.ForRead)
myBTR =
myBT(BlockTableRecord.PaperSpace).GetObject(OpenMode.ForWrite)


strNomeBlocco = "ATTRIBUTI"
For y = 1 To myBlocks.Count
myBlockIDs = PopulateOIDCs(myBlocks(y - 1))
For J = 1 To myBlockIDs.Count
myBlockRef = myBlockIDs(J -
1).GetObject(DatabaseServices.OpenMode.ForWrite)
If UCase(myBlockRef.Name) = strNomeBlocco Then



Dim attCol As AttributeCollection =
myBlockRef.AttributeCollection

For Each attId As ObjectId In attCol
Dim att As AttributeReference =
TryCast(myTrans.GetObject(attId, OpenMode.ForWrite, False),
AttributeReference)
If att.Tag = "TITOLO_RIGA_1" Then
att.UpgradeOpen()
att.TextString = "prova"
att.Dispose()
End If
Next


End If

Next
Next

myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
Message 8 of 29
scarta
in reply to: scarta

Sorry..
I have find the function on the book "vb.net programming for autocad"


Function PopulateOIDCs(ByVal BlockName As String) _
As DatabaseServices.ObjectIdCollection
Dim myBT As DatabaseServices.BlockTable
Dim myBTR As DatabaseServices.BlockTableRecord
Dim myBTRE As DatabaseServices.SymbolTableEnumerator
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myOIDs As New DatabaseServices.ObjectIdCollection
Dim myOIDs2 As DatabaseServices.ObjectIdCollection
Dim I As Long
myDB = ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction
myBT = myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
myBTR = myBT(BlockName).GetObject(DatabaseServices.OpenMode.ForRead)
myOIDs2 = myBTR.GetBlockReferenceIds(True, False)
For I = 1 To myOIDs2.Count
myOIDs.Add(myOIDs2.Item(I - 1))
Next
myBTRE = myBT.GetEnumerator
While myBTRE.MoveNext
myBTR = myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout = False Then
Select Case myBTR.IsAnonymous
Case True
myOIDs2 = myBTR.GetBlockReferenceIds(True, False)
If myOIDs2.Count > 0 Then
Dim myBRef As DatabaseServices.BlockReference
myBRef = myOIDs2(0).GetObject(DatabaseServices.OpenMode.ForRead)
Dim myBTR2 As DatabaseServices.BlockTableRecord
myBTR2 = myBRef.DynamicBlockTableRecord.GetObject( _
DatabaseServices.OpenMode.ForRead)

If myBTR2.Name.CompareTo(BlockName) = 0 Then
For I = 1 To myOIDs2.Count
myOIDs.Add(myOIDs2.Item(I - 1))
Next
End If
End If
End Select
End If
End While
myTrans.Dispose()
myTransMan.Dispose()
Return myOIDs
End Function
Message 9 of 29
Anonymous
in reply to: scarta

I wouldn't use that code if I were you, and I won't get into exactly
why, except to say that amongst other things, it does way more
work than needed to do what you need to do.

The attached code includes a helper function I've used
in the past to update all occurences of a given attribute in
a given block, converted to VB. There's also an example
command showing how to use it.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message
news:6298511@discussion.autodesk.com...
Sorry..
I have find the function on the book "vb.net programming for autocad"


Function PopulateOIDCs(ByVal BlockName As String) _
As DatabaseServices.ObjectIdCollection
Dim myBT As DatabaseServices.BlockTable
Dim myBTR As DatabaseServices.BlockTableRecord
Dim myBTRE As DatabaseServices.SymbolTableEnumerator
Dim myDB As DatabaseServices.Database
Dim myTransMan As DatabaseServices.TransactionManager
Dim myTrans As DatabaseServices.Transaction
Dim myOIDs As New DatabaseServices.ObjectIdCollection
Dim myOIDs2 As DatabaseServices.ObjectIdCollection
Dim I As Long
myDB =
ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
myTransMan = myDB.TransactionManager
myTrans = myTransMan.StartTransaction
myBT =
myDB.BlockTableId.GetObject(DatabaseServices.OpenMode.ForRead)
myBTR = myBT(BlockName).GetObject(DatabaseServices.OpenMode.ForRead)
myOIDs2 = myBTR.GetBlockReferenceIds(True, False)
For I = 1 To myOIDs2.Count
myOIDs.Add(myOIDs2.Item(I - 1))
Next
myBTRE = myBT.GetEnumerator
While myBTRE.MoveNext
myBTR =
myBTRE.Current.GetObject(DatabaseServices.OpenMode.ForRead)
If myBTR.IsLayout = False Then
Select Case myBTR.IsAnonymous
Case True
myOIDs2 = myBTR.GetBlockReferenceIds(True, False)
If myOIDs2.Count > 0 Then
Dim myBRef As DatabaseServices.BlockReference
myBRef =
myOIDs2(0).GetObject(DatabaseServices.OpenMode.ForRead)
Dim myBTR2 As DatabaseServices.BlockTableRecord
myBTR2 =
myBRef.DynamicBlockTableRecord.GetObject( _
DatabaseServices.OpenMode.ForRead)

If myBTR2.Name.CompareTo(BlockName) = 0 Then
For I = 1 To myOIDs2.Count
myOIDs.Add(myOIDs2.Item(I - 1))
Next
End If
End If
End Select
End If
End While
myTrans.Dispose()
myTransMan.Dispose()
Return myOIDs
End Function
Message 10 of 29
scarta
in reply to: scarta

Thank You!!
Now wiht your help I have resolved it, but this code change attribute value for all instance of the block. I would change only for a specified layout..Is this possible?

Stefano
Message 11 of 29
Anonymous
in reply to: scarta

You'll have to figure out how to modify the code to do that.

One way to do it, is like this:

1. Get the value of the Layout object's BlockTableRecordId property,
which is the ObjectId of the block that contains the layout objects.

2. In the code that opens each block reference, compare the
ObjectId for the Layout's BlockTableRecord that you got as
per step 1 above, to the value of each BlockReference's BlockId
property. If they are equal, the block reference is on the Layout
containing the blocks you want to update, and you can modify
the block's attributes. Otherwise, just skip the BlockReference
and go on to the next one.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message
news:6305009@discussion.autodesk.com...
Thank You!!
Now wiht your help I have resolved it, but this code change attribute value
for all instance of the block. I would change only for a specified
layout..Is this possible?

Stefano
Message 12 of 29
Anonymous
in reply to: scarta

I think the problem with this forum search engine is that it searches last 90 days by default. I've been caught a number of times thinking a topic wasn't covered, but when removing that filter it brought back a ton of information.
Message 13 of 29
Anonymous
in reply to: scarta

Autodesk's forum search seems to be designed to not find what you're looking
for.

I use Goggle instead.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD
Supporting AutoCAD 2000 through 2010

http://www.acadxtabs.com

Email: string.Format("{0}@{1}.com", "tonyt", "caddzone");

wrote in message news:6305931@discussion.autodesk.com...
I think the problem with this forum search engine is that it searches last
90 days by default. I've been caught a number of times thinking a topic
wasn't covered, but when removing that filter it brought back a ton of
information.
Message 14 of 29
Anonymous
in reply to: Anonymous

 

Hi Gile,

I want to update Block attributes values without explode process. After attributes updation, the block has been exploded. If i go and search that block in the drawing its not displayed due to exploded.

But i want update attributes without explode the block.

Please help me to go further process.

Thnks
Balaji 

 

Message 15 of 29
_gile
in reply to: Anonymous

Hi,

 

No need to explode the block reference to edit attribute reference values.

There're many examples here and elsewhere.

 

The process is:

Open the BlockReference

For each ObjectId in the BlockReference.AttributeCollection:

    Open the AttributeReference

    If the AttributeReference.Tag matches:

        Set the AttributeReference.TextString value

 

You should show what you have done so far, so that anyone over here (not only me) can help you.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 16 of 29
Anonymous
in reply to: _gile

Hi gile

 

Sorry, i forget to add my code.

 

Please check my code what i done so far.

 

Sub OpenDrwg()

 

Dim objdoc As Object
Dim ListItem As Object
Dim dwgfile, dwgfilelog
Dim filelist, flpth, i

 

actdoc = Nothing
actdb = Nothing
actdb = New Database


Dim docmgr As DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
DocumentCollectionExtension.CloseAll(docmgr)

 

actdoc = DocumentCollectionExtension.Open(docmgr, "D:\Drawingconversion\Batch-7\Input\STER-DRIEHOEK BEDIENING OP 2 LOCATIES.DWG")

actdoc.LockDocument()

 

Dim doc As Document = Application.DocumentManager.MdiActiveDocument

Dim id As ObjectId = GetBlockTableRecordId(doc.Database, "M112")

Dim tag As String = "LC"

Dim newValue As String = "EXTERN-INFO"
Using tr As Transaction = doc.TransactionManager.StartTransaction
Dim btr As BlockTableRecord = TryCast(id.GetObject(OpenMode.ForRead), BlockTableRecord)
Dim cnt As Integer = GlobalModule.SetAttributeValue(btr, tag, newValue)
doc.Editor.WriteMessage(vbNewLine & "Updated {0} attributes", New Object() {cnt})

'btr.Explodable = False
tr.Commit()
'tr.Dispose()
End Using

'actdoc.Database.Save()
actdoc.Database.SaveAs("D:\Drawingconversion\Batch-7\Input\STER-DRIEHOEK BEDIENING OP 2 LOCATIES-2.DWG", True, DwgVersion.Current, actdoc.Database.SecurityParameters)

DocumentCollectionExtension.CloseAll(docmgr)

 

End Sub

 

 

 

Public Function SetAttributeValue(ByVal btr As BlockTableRecord, ByVal tag As String, ByVal newValue As String) As Integer

 

Dim cnt As Integer = 0

Using tr As Transaction = btr.Database.TransactionManager.StartTransaction

 

Dim ids As ObjectIdCollection = btr.GetBlockReferenceIds(True, True)

 

If (Not ids Is Nothing) Then

 

Dim btrId As ObjectId

 

For Each btrId In ids
Dim blockref As BlockReference = TryCast(btrId.GetObject(OpenMode.ForRead, False), BlockReference)

 

If (Not blockref.AttributeCollection Is Nothing) Then

Dim attId As ObjectId

 

For Each attId In blockref.AttributeCollection
Dim att As AttributeReference = TryCast(attId.GetObject(OpenMode.ForRead, False), AttributeReference)
If String.Equals(att.Tag, tag, StringComparison.OrdinalIgnoreCase) Then
Try
att.UpgradeOpen()
MsgBox(newValue)
att.TextString = newValue
att.DowngradeOpen()
'att.Dispose()

Catch ex As Global.Autodesk.AutoCAD.Runtime.Exception
If (ex.ErrorStatus <> ErrorStatus.OnLockedLayer) Then
Throw

 

End If

Exit For

End Try
cnt += 1
Exit For

 

End If

 

Next
blockref.ExplodeToOwnerSpace()
End If
Next
End If
tr.Commit()
'tr.Dispose()
End Using
Return cnt
End Function

 

My process is :

1. Open drawing file

2. Check the block and Tag

3. Update the value to particular attribute

4. Saveas file

 

After complition that process, if open saved file, my updated block is not available because of my block has been exploded.

 

I don't want that block to be exploded. 

 

Values should be update without explode method.

 

Please help me to do without explode process.

 

 

Thnks

Balaji

 

 

 

Message 17 of 29
_gile
in reply to: Anonymous


@Anonymous wrote:

Public Function SetAttributeValue(ByVal btr As BlockTableRecord, ByVal tag As String, ByVal newValue As String) As Integer

 

Dim cnt As Integer = 0

Using tr As Transaction = btr.Database.TransactionManager.StartTransaction

 

Dim ids As ObjectIdCollection = btr.GetBlockReferenceIds(True, True)

 

If (Not ids Is Nothing) Then

 

Dim btrId As ObjectId

 

For Each btrId In ids
Dim blockref As BlockReference = TryCast(btrId.GetObject(OpenMode.ForRead, False), BlockReference)

 

If (Not blockref.AttributeCollection Is Nothing) Then

Dim attId As ObjectId

 

For Each attId In blockref.AttributeCollection
Dim att As AttributeReference = TryCast(attId.GetObject(OpenMode.ForRead, False), AttributeReference)
If String.Equals(att.Tag, tag, StringComparison.OrdinalIgnoreCase) Then
Try
att.UpgradeOpen()
MsgBox(newValue)
att.TextString = newValue
att.DowngradeOpen()
'att.Dispose()

Catch ex As Global.Autodesk.AutoCAD.Runtime.Exception
If (ex.ErrorStatus <> ErrorStatus.OnLockedLayer) Then
Throw

 

End If

Exit For

End Try
cnt += 1
Exit For

 

End If

 

Next
blockref.ExplodeToOwnerSpace()
End If
Next
End If
tr.Commit()
'tr.Dispose()
End Using
Return cnt
End Function

 


Did you try removing: blockref.ExplodeToOwnerSpace()?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 18 of 29
Anonymous
in reply to: _gile

Hi gile,

 

Thanks for your immediate reply.

 

Even if remove blockref.ExplodeToOwnerSpace()

 

I am facing same problem, after upation block attributes. That block has been exploded.

 

But i don't want to be exploded. 

 

What i am looking is, i want to open drawing file and update particular block/tag attribute values and save as new dwg. Open saved dwg and check whether block/tag attributes has changed.

 

Currently,i can able to find block/tag and update the value. 

 

is it correct coding(i posted) for what i am looking my process.??

 

Thnks

Balaji

 

Message 19 of 29
_gile
in reply to: Anonymous

From the code you show, after removing blockref.ExplodeToOwnerSpace(), there's no reason for the block reference to be exploded.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 20 of 29
Anonymous
in reply to: _gile

Hi Gile,

 

Once again i will check with my code, if i done any mistake, i will correct and update you.

 

 

 

 

Thnks

Balaji

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