[VB.net] Change part appearance

[VB.net] Change part appearance

ChristianAndersenIsmyname
Advocate Advocate
1,364 Views
4 Replies
Message 1 of 5

[VB.net] Change part appearance

ChristianAndersenIsmyname
Advocate
Advocate

Hi, can anyone give me a helping hand for changing the appearance of the active part with VB.net?

 

I've looked through many posts, but can't seem to find something that works.

 

I think this code below explains what I want to achieve:

Dim invapp As Inventor.Application
invapp = GetObject(, "Inventor.Application")
Dim Doc As Document
Doc = invapp.ActiveDocument

'Grabbing row info from database

If ComboBox1.Text = row(1) Then
                
     'row(1) = material
     'row(2) = density
     'row(3) = appearance

     Doc.appearance = row(3)

End If

 Row(3) is a copy of the displaynames of the appearances in Autodesk Appearance Library.

 

While searching for answers to this, I came across several posts for changing the appearance of the part occurence in assemblies, but I can't get codes like these to work:

 

Dim oAsset As Asset
oAsset = Doc.Assets.Item("Smooth - Red")
Doc.Appearance = oAsset

 

Thanks for all the help!

0 Likes
Accepted solutions (1)
1,365 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

The "ActiveAppearance" property becomes available after you have identified the document object as either a PartDocument or an AssemblyDocument.  Then you can also access the "AppearanceAssets" property, which you can use to help set the one you want.

 

Assuming the returned data type being represented by "row(3)" is a String...

Maybe try something similar to this:

Dim invapp As Inventor.Application = GetObject(, "Inventor.Application")
Dim oDocType As DocumentTypeEnum = invapp.ActiveDocumentType
Dim oPDoc As PartDocument
Dim oADoc As AssemblyDocument
If oDocType = DocumentTypeEnum.kPartDocumentObject Then
	oPDoc = invapp.ActiveDocument
ElseIf oDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
	oADoc = invapp.ActiveDocument
Else
	MsgBox("This code only works for Part or Assembly Documents.",vbOKOnly, "WRONG DOCUMENT TYPE")
	Exit Sub
End If

If ComboBox1.Text = row(1) Then     
     'row(1) = material
     'row(2) = density
     'row(3) = appearance
     oPDoc.ActiveAppearance = oPDoc.AppearanceAssets.Item(row(3))
End If

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

ChristianAndersenIsmyname
Advocate
Advocate

Hello WCrihfield!

This feature will only be available in partDocuments, so I took the freedom to clean up the code a bit, and I'm left with this:

        Dim invapp As Inventor.Application = GetObject(, "Inventor.Application")
        Dim DocType As DocumentTypeEnum = invapp.ActiveDocumentType
        Dim PDoc As PartDocument
        PDoc = invapp.ActiveDocument
'Database codes
        For Each row In t1.Rows
            If ComboBox1.Text = row(1) Then

                PDoc.ActiveAppearance = PDoc.AppearanceAssets.Item(row(3))
            End If
        Next

 

But I'm getting a error. "Wrong Parameter. (Exception ... )"

bilde.png

For this selected material, the database row(3) = Machined - Aluminum

0 Likes
Message 4 of 5

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @ChristianAndersenIsmyname 

I think your problem is that the appearance doesn't exist in the documents appearance assets. If it doesn't, you'll have to copy it from the Autodesk Appearance Library to your document. See example iLogic code below 🙂

 

Dim pDoc As PartDocument = ThisDoc.Document
'Just for this example, row(3) is set to "Machined - Aluminum"
Dim row(3) As String
row(3) = "Machined - Aluminum"

Try
'If the appearance exists in the document appearances, this will work
pDoc.ActiveAppearance = pDoc.AppearanceAssets.Item(row(3))
Catch
'Get the library
Dim oLib As AssetLibrary = ThisApplication.AssetLibraries.Item("Autodesk Appearance Library")
'MsgBox("Library found!")

'Get the appearance
Dim oApp As Asset = oLib.AppearanceAssets.Item(row(3))
'MsgBox("Asset Found found!")

'Copy appearance to document
oApp.CopyTo(pDoc, False)

'Set the appearance of the document
pDoc.ActiveAppearance = oApp
End Try
Message 5 of 5

ChristianAndersenIsmyname
Advocate
Advocate

This did the trick! Thank you 🙂

 

        Dim invapp As Inventor.Application = GetObject(, "Inventor.Application")
        Dim PDoc As PartDocument
        PDoc = invapp.ActiveDocument

        Dim query As String = "SELECT * FROM " & Line(10)
        Dim ds As New DataSet
        Dim cnn As OleDbConnection = New OleDbConnection(MDBConnString_)
        cnn.Open()
        Dim cmd As New OleDbCommand(query, cnn)
        Dim da As New OleDbDataAdapter(cmd)
        da.Fill(ds, Line(1))
        cnn.Close()
        Dim t1 As DataTable = ds.Tables(Line(1))
        Dim row As DataRow
        For Each row In t1.Rows
            If ComboBox1.Text = row(1) Then
                Try
                    PDoc.ActiveAppearance = PDoc.AppearanceAssets.Item(row(3))
                Catch ex As Exception
                    Dim oLib As AssetLibrary = invapp.AssetLibraries.Item("Autodesk Appearance Library")
                    Dim oApp As Asset = oLib.AppearanceAssets.Item(row(3))
                    oApp.CopyTo(PDoc, False)
                    PDoc.ActiveAppearance = oApp
                End Try
            End If
        Next

 

0 Likes