VBA fetching wrong iProperty

VBA fetching wrong iProperty

ChristianAndersenIsmyname
Advocate Advocate
855 Views
7 Replies
Message 1 of 8

VBA fetching wrong iProperty

ChristianAndersenIsmyname
Advocate
Advocate

Hi,

In my job I have to create drawings with multiple parts (up to 50 parts per drawing), and therefore I have made a VBA Macro thats working almost as intended, only issue now is that its selecting the Part Number from the .idw iProperty, instead of the part that's been viewed in the drawing.

 

Picture below is showing which Part Number the code should give me, and which Part Number it is giving me.

PartNumberMacro.jpg

To get Part Number, Ive copied code from https://modthemachine.typepad.com/my_weblog/2010/02/accessing-iproperties.html. Giving me the code lines:

' Get the active document.
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

' Get the PropertySets object.
Dim oPropSets As PropertySets
Set oPropSets = oDoc.PropertySets

' Get the design tracking property set.
Dim oPropSet As PropertySet
Set oPropSet = oPropSets.Item("Design Tracking Properties")

' Get the part number iProperty.
Dim oPartNumiProp As Property
Set oPartNumiProp = oPropSet.Item("Part Number")

Later in the Macro, I have this line:

oView.Label.FormattedText = oPartNumiProp.Value & " ( <DrawingViewScale/> )"

 

Full VBA code here: https://justpaste.it/789et

 

I've tried to google, and read many forum post, but as far as I can see, no one is asking this specifically.

 

I'm sorry for spamming and bumping similar post the last few days.

Any help is appriciated, thanks in advance.

0 Likes
Accepted solutions (1)
856 Views
7 Replies
Replies (7)
Message 2 of 8

andrewiv
Advisor
Advisor

Your code is only looking at the drawing.  You need to tell it to grab the iproperty from the referenced document.  This code is in iLogic but all you should have to do to convert to VBA is add Set before the variable definitions.

' Get the active document.
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDoc.ActiveSheet
Dim oDwgView As DrawingView
oDwgView = oSheet.DrawingViews.Item(1)
Dim oModelDoc As Document
oModelDoc = oDwgView.ReferencedDocumentDescriptor.ReferencedDocument

' Get the PropertySets object.
Dim oPropSets As PropertySets
oPropSets = oModelDoc.PropertySets

' Get the design tracking property set.
Dim oPropSet As PropertySet
oPropSet = oPropSets.Item("Design Tracking Properties")

' Get the part number iProperty.
Dim oPartNumiProp As Inventor.Property
oPartNumiProp = oPropSet.Item("Part Number")

 

Andrew In’t Veld
Designer / CAD Administrator

Message 3 of 8

ChristianAndersenIsmyname
Advocate
Advocate

Thank you for a explained answer @andrewiv.

This is one step closer, but its now fetching the assembly part name.

Should probably have mentioned in original post that this is assembly drawing with its parts included in the same idw.

 

N-50817 is the part name of the Assembly

macro.PNG

0 Likes
Message 4 of 8

andrewiv
Advisor
Advisor

I took a look at your full code and I think I know how to fix it.  Right now, my code just grabs the model from the first view on the active sheet.  In your code you have a pick view event and you will have to substitute that in for the oDwgView definition.  That should grab the part for the oModelDoc variable.

Andrew In’t Veld
Designer / CAD Administrator

0 Likes
Message 5 of 8

ChristianAndersenIsmyname
Advocate
Advocate

Could you elaborate this is little bit more? Or possibly even write the code?

I've tried a little bit, but only got errors or warnings about duplicates. So I must be doing something wrong.

As you might have understood, I have no experience with coding or VBA in general.

0 Likes
Message 6 of 8

andrewiv
Advisor
Advisor
Accepted solution

Try this.  Again it's in iLogic so if you need it in VBA you'll have to convert.

Sub Main
' Get the active document.
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDoc.ActiveSheet
Dim oView As DrawingView
Dim oModelDoc As Document
Dim oPropSets As PropertySets
Dim oPropSet As PropertySet
Dim oPartNumiProp As Inventor.Property

For i = 1 To 1000

	oView = ThisApplication.CommandManager.Pick(kDrawingViewFilter,"Select drawing view")
	
	If oView Is Nothing Then
		Exit Sub
	End If
	
	oModelDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
	oPropSets = oModelDoc.PropertySets
	oPropSet = oPropSets.Item("Design Tracking Properties")
	oPartNumiProp = oPropSet.Item("Part Number")
	oView.Label.FormattedText = oPartNumiProp.Value & " ( <DrawingViewScale/> )"
	
	If oView.ShowLabel = False Then
		oView.ShowLable = True
	End If
	
Next i

End Sub

 

Andrew In’t Veld
Designer / CAD Administrator

Message 7 of 8

ChristianAndersenIsmyname
Advocate
Advocate

Thank you very much!

Had to make some adjustments, but its now working as it should!

 

If anyone else need it later, heres the full code:

Sub Testmacro()

'---------------------------------------------------------
'   Integer to select multiple view
'---------------------------------------------------------

Dim i As Integer

'---------------------------------------------------------
'   Fetching Part Number from iProperties
'---------------------------------------------------------

' Get the active document.
Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
Set oSheet = oDoc.ActiveSheet
Dim oView As DrawingView
Dim oModelDoc As Document
Dim oPropSets As PropertySets
Dim oPropSet As PropertySet
Dim oPartNumiProp As Inventor.Property

'---------------------------------------------------------
'   Prompt to pick drawing view & Start loop
'---------------------------------------------------------

For i = 1 To 1000
    Set oView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select drawing view")

    If oView Is Nothing Then
        Exit Sub
    End If


'---------------------------------------------------------
'   Cancel macro
'---------------------------------------------------------

If oView Is Nothing Then
    Exit Sub
End If

'---------------------------------------------------------
'   Label text
'---------------------------------------------------------

Set oModelDoc = oView.ReferencedDocumentDescriptor.ReferencedDocument
Set oPropSets = oModelDoc.PropertySets
Set oPropSet = oPropSets.Item("Design Tracking Properties")
Set oPartNumiProp = oPropSet.Item("Part Number")
oView.Label.FormattedText = oPartNumiProp.Value & " ( <DrawingViewScale/> )"

'---------------------------------------------------------
'   View visibility
'---------------------------------------------------------

If oView.ShowLabel = False Then
  oView.ShowLabel = True
Else
  oView.ShowLabel = True
End If

'---------------------------------------------------------
'   Repeat loop
'---------------------------------------------------------

Next i

'---------------------------------------------------------
'   End Macro
'---------------------------------------------------------

End Sub

 

0 Likes
Message 8 of 8

andrewiv
Advisor
Advisor


You can get rid of the first statement that says

    If oView Is Nothing Then
        Exit Sub
    End If

With both of them in there you're telling Inventor to do the same thing two times in a row.

 

You can also get rid of the Else statement in

If oView.ShowLabel = False Then
  oView.ShowLabel = True
Else
  oView.ShowLabel = True
End If

If the oView.ShowLabel is not false then it is already true

Andrew In’t Veld
Designer / CAD Administrator