Inventor Customization

Reply
Contributor
VidarDK
Posts: 11
Registered: ‎08-14-2007
Message 1 of 9 (467 Views)
Accepted Solution

Edit AutoCAD attributes

467 Views, 8 Replies
09-05-2011 05:26 AM

We have a program to update our titleblocks, the following code has worked without problems in Inventor 2010.
But in Inventor 2012, it doesn't work every time. When I insert the titleblock for the first time I can fill the attributes,
but when I want to update the titleblock, I see no change, although it has changed the values, which I can see if I manually do an edit attributes (see screenshot).

 

Any ideas?

Thanks,

Michael

 

    Public Shared Function SaveAutocadAttributes(ByVal intDocID As Integer, ByRef invApp As Inventor.Application) As Boolean
      Dim ht As Hashtable = AcadAttributesMapping(intDocID, invApp)
      Dim i As Integer
      Dim invDoc As DrawingDocument
      Dim acadDoc As AcadDatabase
      Dim objBlock As AcadBlock = Nothing
      Dim objAcadEntity As AcadEntity = Nothing
      Dim objAcadBlockReference As AcadBlockReference = Nothing
      Dim obj() As Object = Nothing
      Dim objAcadObject As AcadObject = Nothing

      invDoc = invApp.ActiveDocument
      acadDoc = invDoc.ContainingDWGDocument
      For Each objBlock In acadDoc.Blocks
        For Each objAcadEntity In objBlock
          If objAcadEntity.ObjectName.ToLower = "AcDbBlockReference".ToLower Then
            objAcadBlockReference = CType(objAcadEntity, AcadBlockReference)
            If objAcadBlockReference.HasAttributes Then
              Select Case objAcadBlockReference.Name
                Case "A0", "A1", "A2", "A3", "A3D", "A4", "P0", "P1", "P2", "P3", "P4"
                  obj = DirectCast(objAcadBlockReference.GetAttributes, Object())
                  For i = LBound(obj) To UBound(obj)
                    If ht.ContainsKey(obj(i).TagString) = True Then
                      obj(i).TextString = ht.Item(obj(i).TagString)
                    End If
                  Next i
              End Select
            End If
          End If
        Next
      Next
      invDoc.Update()
    End Function

 

 

The following code code works for me.

 

    Public Shared Sub SaveAttributes(ByVal strFieldVaule() As String, ByVal strFieldName() As String, ByRef invApp As Inventor.Application)
      Dim myBlockRef As AcadBlockReference
      Dim i As Short
      Dim h As Short
      Dim myAttributes As Object
      Dim invDoc As DrawingDocument
      Dim acadDoc As AcadDatabase

      invDoc = invApp.ActiveDocument
      acadDoc = invDoc.ContainingDWGDocument
      For Each Entity In acadDoc.PaperSpace
        If TypeOf Entity Is AcadBlockReference Then
          myBlockRef = Entity
          Select Case myBlockRef.Name
            Case "A0", "A1", "A2", "A3", "A4", "P0", "P1", "P2", "P3", "P4"
              myAttributes = Entity.GetAttributes
              For i = LBound(myAttributes) To UBound(myAttributes)
                For h = LBound(strFieldName) To UBound(strFieldName)
                  If strFieldName(h) = myAttributes(i).TagString Then
                    myAttributes(i).TextString = strFieldVaule(h)
                  End If
                Next
              Next i
          End Select
        End If
      Next
      invDoc.Update()
    End Sub

 AcadAttributesMapping was just a function that i made to put attribute values to a Hashtable.

Valued Contributor
RyanBotha
Posts: 98
Registered: ‎01-15-2007
Message 2 of 9 (413 Views)

Re: Edit AutoCAD attributes

04-04-2012 12:47 AM in reply to: VidarDK

Hey Michael,

 

Did you ever get this to work?

 

What namespace does AcadAttributesMapping belong to?

 

Thank you

 

Contributor
VidarDK
Posts: 11
Registered: ‎08-14-2007
Message 3 of 9 (393 Views)

Re: Edit AutoCAD attributes

04-10-2012 12:51 AM in reply to: RyanBotha

The following code code works for me.

 

    Public Shared Sub SaveAttributes(ByVal strFieldVaule() As String, ByVal strFieldName() As String, ByRef invApp As Inventor.Application)
      Dim myBlockRef As AcadBlockReference
      Dim i As Short
      Dim h As Short
      Dim myAttributes As Object
      Dim invDoc As DrawingDocument
      Dim acadDoc As AcadDatabase

      invDoc = invApp.ActiveDocument
      acadDoc = invDoc.ContainingDWGDocument
      For Each Entity In acadDoc.PaperSpace
        If TypeOf Entity Is AcadBlockReference Then
          myBlockRef = Entity
          Select Case myBlockRef.Name
            Case "A0", "A1", "A2", "A3", "A4", "P0", "P1", "P2", "P3", "P4"
              myAttributes = Entity.GetAttributes
              For i = LBound(myAttributes) To UBound(myAttributes)
                For h = LBound(strFieldName) To UBound(strFieldName)
                  If strFieldName(h) = myAttributes(i).TagString Then
                    myAttributes(i).TextString = strFieldVaule(h)
                  End If
                Next
              Next i
          End Select
        End If
      Next
      invDoc.Update()
    End Sub

 AcadAttributesMapping was just a function that i made to put attribute values to a Hashtable.

Valued Contributor
RyanBotha
Posts: 98
Registered: ‎01-15-2007
Message 4 of 9 (388 Views)

Re: Edit AutoCAD attributes

04-10-2012 03:32 AM in reply to: VidarDK

What a legend! Thank you! 

Valued Contributor
dave.deane
Posts: 78
Registered: ‎04-27-2007
Message 5 of 9 (318 Views)

Re: Edit AutoCAD attributes

12-26-2012 04:37 AM in reply to: VidarDK

I am trying to generate some code to read an Autocad created titleblock with attributes that is in a Inventor 2013 .dwg using Visual Studio Express 2010. However, I have three references "not defined" when starting with the code above.

 

I have referenced the Inventor's interop library, am I missing another reference? Please see attached screenshot..

 

AcadBlockReference, DrawingDocument, AcadDatabase are the ones missing.

 

Thanks,

Dave

Contributor
VidarDK
Posts: 11
Registered: ‎08-14-2007
Message 6 of 9 (283 Views)

Re: Edit AutoCAD attributes

01-02-2013 12:04 AM in reply to: dave.deane

You have to reference

Autodesk.AutoCAD.Interop

and

Autodesk.AutoCAD.Interop.Common

 
Michael
Valued Contributor
dave.deane
Posts: 78
Registered: ‎04-27-2007
Message 7 of 9 (270 Views)

Re: Edit AutoCAD attributes

01-02-2013 04:41 AM in reply to: VidarDK

Thank you for replying Michael but oddly enough the reference is not defined after adding in autodesk.AutoCAD.Interop and Autodesk.AutoCAD.Interop.Common. Screen shot below.

 

Using Visual Studio 2010 express.

 

Dave

Contributor
VidarDK
Posts: 11
Registered: ‎08-14-2007
Message 8 of 9 (263 Views)

Re: Edit AutoCAD attributes

01-02-2013 05:29 AM in reply to: dave.deane

You need to put the imports namespace names in the code as well.

 

Imports Autodesk.AutoCAD.Interop.Common

Valued Contributor
dave.deane
Posts: 78
Registered: ‎04-27-2007
Message 9 of 9 (243 Views)

Re: Edit AutoCAD attributes

01-02-2013 10:15 AM in reply to: VidarDK

Thank you...it is working now.

 

Dave

Spoiler
 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.