iLogic - Sum of a Custom iProperty Without Adding the Suppressed Components

iLogic - Sum of a Custom iProperty Without Adding the Suppressed Components

Anonymous
Not applicable
2,172 Views
13 Replies
Message 1 of 14

iLogic - Sum of a Custom iProperty Without Adding the Suppressed Components

Anonymous
Not applicable

Hi,

 

Please see the code that I have been working through below.

 

The goal is to sum up the total of a custom iProperty called MSRP.

 

I seem to have everything working well however the moment I suppress something I get an Unspecified Error.

 

Any help would be greatly appreciated,

 

Riese

 

 

 

SyntaxEditor Code Snippet

'- - - - - - - - - - - find or create custom iProperty - - - - - - - - - -
'Define the open document & make Library Files Editable (IV 2018)
ThisApplication.[_LibraryDocumentModifiable] = True
Dim openDoc As Document
openDoc = ThisDoc.Document
'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
Dim propertyName1 As String = "MSRP"
'define custom property collection
oCustomPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")
Try
'look for property
oProp = oCustomPropertySet.Item(propertyName1)
Catch
' Assume error means not found so create it
oCustomPropertySet.Add(0, propertyName1)
End Try
Next
'- - - - - - - - - - - sum the custom iProperty - - - - - - - - - -
'clear the custom property in the assembly
iProperties.Value("Custom", "MSRP") = 0
'set a reference to the assembly component definintion.
'This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
'check for and skip virtual components
'(in case a virtual component trips things up)
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
'Property In the Assembly
xNumber = iProperties.Value("Custom", "MSRP")
'property in the parts
yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MSRP")
sumNumber = xNumber + yNumber
'set property values
iProperties.Value("Custom", "MSRP") = Round (sumNumber,2)
MSRP = sumNumber
Else
End If
Next

 

 

0 Likes
Accepted solutions (1)
2,173 Views
13 Replies
Replies (13)
Message 2 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

iLogic code looks fine for me. Can you please specify "what are you trying to suppress to get unspecified error"?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 14

MechMachineMan
Advisor
Advisor

The issue is because a suppressed file has reduced load in memory, so I think not all API of it is accessible.

 

You should be able to add in a test and either unsuppress/measure/resuppress, or skip the file according.

 

http://help.autodesk.com/view/INVNTOR/2018/ENU/?guid=GUID-75CCD20D-728B-4B77-B950-263E297A68EC

 

Also helps for readability/reusability/general programming to properly encapsulate the code (splitting it into logical sub-routines) instead of leaving it as a procedural code.

 

Sub Main()
ThisApplication
.[_LibraryDocumentModifiable] = True Dim openDoc As Document openDoc = ThisDoc.Document
Call SetInitialMSRPValues(openDoc)
Call SumMSRP(openDoc as Document)
End Sub
Sub SetInitialMSRPValues(openDoc As Document)
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
Dim propertyName1 As String = "MSRP"
oCustomPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")

Try
oProp
= oCustomPropertySet.Item(propertyName1)
Catch
oCustomPropertySet.Add(0, propertyName1)
End Try
Next
End Sub

Sub SumMSRP(openDoc as Document)

iProperties.Value("Custom", "MSRP") = 0
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = openDoc.ComponentDefinition

Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
If oOccurrence.Suppressed = True
MsgBox(oOccurrence.Name & " is suppressed. Excluding from MRSP!")
Else
xNumber = iProperties.Value("Custom", "MSRP")
yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MSRP")
sumNumber = xNumber + yNumber
iProperties.Value("Custom", "MSRP") = Round (sumNumber,2)
MSRP = sumNumber
End if 'Suppressed

End If 'Virtual Component
Next
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 4 of 14

Anonymous
Not applicable

Hi Justin,

 

Thanks for your reply and the suggested solution, I found it very helpful how you had organized the the code into logical sub routines.

I have a number of years experience with Inventor and the Vault Pro environments , but have only now just begun taking courses in VBA and iLogic.

So for the moment my scripting knowledge is still quite limited but anything that I can pick up or learn is greatly appreciated.

 

I tested what you had provided as a solution but ran into a small error that I am not able to fix myself.

 

In the statement 

Call SumMSRP(openDoc as Document)

the As calls up  the following error " ' ) ' expected"

 

Please see the attached screenshot.

 

I have also included a small zipped up version of the assembly that I am working with if that helps.

 

If we can get this working properly it should be of benefit to many others.

 

Thanks,

 

Riese

 

 

 

 

0 Likes
Message 5 of 14

Anonymous
Not applicable

Hi Chandra,

 

I appreciate you looking at the iLogic code this morning.

 

I created a little video and placed it on A360 at http://a360.co/2mepX0F as an illustration to help demonstrate the error on a small sample project.

 

(The files are all zipped up and included in my reply to Kevin BTW)

 

I appreciate the solution offered by Kevin, but could this code be made to work with iLogic, all by it self?

 

Of course  iLogic has its limitations, compared to VBA, but it would be too bad if using a full blown VBscript is the only solution for this.

 

 

Thanks for any help,

 

Riese

 

 

0 Likes
Message 6 of 14

Anonymous
Not applicable

The 'as' statement shouldn't have been in the functino call.

 

Sub Main()
ThisApplication
.[_LibraryDocumentModifiable] = True Dim openDoc As Document openDoc = ThisDoc.Document
Call SetInitialMSRPValues(openDoc)
Call SumMSRP(openDoc)
End Sub
Sub SetInitialMSRPValues(openDoc As Document)
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments
Dim propertyName1 As String = "MSRP"
oCustomPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")

Try
oProp
= oCustomPropertySet.Item(propertyName1)
Catch
oCustomPropertySet.Add(0, propertyName1)
End Try
Next
End Sub

Sub SumMSRP(openDoc as Document)

iProperties.Value("Custom", "MSRP") = 0
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = openDoc.ComponentDefinition

Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then
If oOccurrence.Suppressed = True
MsgBox(oOccurrence.Name & " is suppressed. Excluding from MRSP!")
Else
xNumber = iProperties.Value("Custom", "MSRP")
yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MSRP")
sumNumber = xNumber + yNumber
iProperties.Value("Custom", "MSRP") = Round (sumNumber,2)
MSRP = sumNumber
End if 'Suppressed

End If 'Virtual Component
Next
End Sub
 
0 Likes
Message 7 of 14

Anonymous
Not applicable

Thanks Justin,

 

I appreciate your help your script works perfectly now except if I suppress a component then I get the following error.

 

 

System.Runtime.InteropServices.COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Inventor.ComponentOccurrence.get_Definition()
at LmiRuleScript.SumMSRP(Document openDoc)
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

Any ideas?

 

Best Regards,

 

Riese

0 Likes
Message 8 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @Anonymous,

 

Try the following iLogic code to calculate MSRP for assembly.

 

Sub Main()
    ThisApplication.[_LibraryDocumentModifiable] = True
    Dim openDoc As Document
    openDoc = ThisDoc.Document
    Call SetInitialMSRPValues(openDoc)
    Call SumMSRP(openDoc)
End Sub

Sub SetInitialMSRPValues(openDoc As Document)
    Dim docFile As Document
    For Each docFile In openDoc.AllReferencedDocuments
        Dim propertyName1 As String = "MSRP" 
        oCustomPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")

        Try  
            oProp = oCustomPropertySet.Item(propertyName1)
        Catch
            oCustomPropertySet.Add(0, propertyName1)
        End Try
    Next
End Sub

Sub SumMSRP(openDoc As Document)

    iProperties.Value("Custom", "MSRP") = 0
    Dim oAsmCompDef As AssemblyComponentDefinition
    oAsmCompDef = openDoc.ComponentDefinition

    Dim oOccurrence As ComponentOccurrence
    For Each oOccurrence In oAsmCompDef.Occurrences
	If oOccurrence.Suppressed = True
            MsgBox(oOccurrence.Name & " is suppressed. Excluding from MRSP!")
        Else				
	        If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then	            
                	xNumber = iProperties.Value("Custom", "MSRP") 
                	yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MSRP")
                	sumNumber = xNumber + yNumber
                	iProperties.Value("Custom", "MSRP") = Round (sumNumber,2)
                	MSRP = sumNumber	             
	        End If 'Virtual Component
	End If 'Suppressed
    Next
End Sub 

Actually, failed to retrieve definition of occurrence if it is suppressed. Before checking virtual component definition, check whether occurrence is suppressed or not.

 

Please feel free to contact if there is any queries.

 

If problem is solved, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 9 of 14

Anonymous
Not applicable

Hi Chandra,

 

What I have noticed now is that if I run the updated rule, I successfully get the notices regarding each suppressed component, the MSRP calculation does not change  and then the error message again pops up again.

 

However if I run the rule a second time it then works perfectly, without any error and the MSRP calculations are correctly updated.

 

We must be close to a solution.

 

Best Regards,

 

Riese

 

0 Likes
Message 10 of 14

MechMachineMan
Advisor
Advisor

So you are saying you want it to calculate the MSRP for the suppressed components too?


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 11 of 14

Anonymous
Not applicable

No, sorry for not being clear.

 

Whatever is 'active' in the custom level of detail,( in my case it is called "MyLOD"), needs to have all of it's custom numeric iProperties "MSRP" summed up as a total.

 

Whatever is suppressed in that custom LOD should just be ignored and not included..

 

Thanks,

 

Riese

0 Likes
Message 12 of 14

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Anonymous,

 

For me, its working fine. Please refer the following screencast for more details.

 

Sub Main()
    ThisApplication.[_LibraryDocumentModifiable] = True
    Dim openDoc As Document
    openDoc = ThisDoc.Document
    Call SetInitialMSRPValues(openDoc)
    Call SumMSRP(openDoc)
    ThisApplication.[_LibraryDocumentModifiable] = False
End Sub

Sub SetInitialMSRPValues(openDoc As Document)
    Dim docFile As Document
    For Each docFile In openDoc.AllReferencedDocuments
        Dim propertyName1 As String = "MSRP" 
        oCustomPropertySet = docFile.PropertySets.Item("Inventor User Defined Properties")
        Try  
            oProp = oCustomPropertySet.Item(propertyName1)
        Catch
            oCustomPropertySet.Add(0, propertyName1)
        End Try
    Next
End Sub

Sub SumMSRP(openDoc As Document)

    iProperties.Value("Custom", "MSRP") = 0
    Dim oAsmCompDef As AssemblyComponentDefinition
    oAsmCompDef = openDoc.ComponentDefinition

    Dim oOccurrence As ComponentOccurrence
    For Each oOccurrence In oAsmCompDef.Occurrences
	If oOccurrence.Suppressed = True
        MsgBox(oOccurrence.Name & " is suppressed. Excluding from MRSP!")
    Else				
	    If Not TypeOf oOccurrence.Definition Is VirtualComponentDefinition Then	            
        	xNumber = iProperties.Value("Custom", "MSRP") 
        	yNumber = iProperties.Value(oOccurrence.Name, "Custom", "MSRP")
        	sumNumber = xNumber + yNumber
        	iProperties.Value("Custom", "MSRP") = Round (sumNumber,2)
        	MSRP = sumNumber	             
        End If 'Virtual Component
	End If 'Suppressed
    Next
End Sub

The only change I made is "ThisApplication.[_LibraryDocumentModifiable] = False".

 

 

 
Thanks and regards,

CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 13 of 14

Anonymous
Not applicable

Hi Chandra & Justin,

 

Thanks very much for your help, patience and extra effort.

 

In my case I do need the statement "ThisApplication.[_LibraryDocumentModifiable] = False", set to "True" since in the production model there are a number of library parts and a release IV 2018.2 "fix" necessitates its inclusion.

 

Having said that, I spent a good part of the day poking around and learning some more.

It seemed that I needed to save the assembly before the MSRP calculator would work properly, since it would fail the first time I ran it, but then work perfectly when I ran it the second time. 

 

But then I tried a completely different approach on my current project leaving your code exactly as is.

 

I used 

ThisApplication.ActiveDocument.ComponentDefinition.RepresentationsManager.LevelofDetailRepresentations("iLogic").Activate

 to set the various LoD's that I require.

 

Then I used a multi-value User Defined Property to activate each of the LoD's, each with its own iteration of the assembly that I am working on.

 

The MSRP calculator now works immediately and perfectly. 

 

I have it updating instantly as any additional modifications are made to each unique LoD.

 

Again I really appreciate the help you have provided and hope it will help some others as well.

 

Appreciatively Yours,

 

Riese

 

 

 

 

 

0 Likes
Message 14 of 14

Anonymous
Not applicable

Hi Chandra,

 

This code is very useful. Is there a way to make it ignore parts that don't have the value we are looking for?

Rather than making it add to the parts.

 

Also, if I wanted to nock off a value {200mm for example}

Would that be possible?

When I add to the end of the code it doesn't subtract, and when I add to the middle it multiples the number by the number of parts I think.

 

Thank you!

0 Likes