iLogic Try Catch w/ Parameters

iLogic Try Catch w/ Parameters

Anonymous
Not applicable
8,884 Views
9 Replies
Message 1 of 10

iLogic Try Catch w/ Parameters

Anonymous
Not applicable

I'm using the rule below at the assembly level to go through all parts in the assembly and change the schedule to whatever is set at the assembly level. I used a Try/Catch statement since not all parts will have the "Sch" parameter - only piping and fittings will have that parameter. Maybe I am not using this statement correctly though? I have message boxes for debugging purposes on both the Try and Catch. When I run the rule, I get the Catch message box for all parts as if it couldn't find that Parameter, but then the parts update as expected anyway! Note, the "Sch" parameter at both the assembly and part levels are multivalue text. Could that have something to do with it?

 

See code below and I've also attached a sample part, so feel free to try it out for yourself. As always, any help is much appreciated!

' Set a reference to the assembly component definintion
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Iterate through all occurrences
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmCompDef.Occurrences
	Try
		Parameter(oOcc.Name,"Sch") = PipeSch
		Parameter(oOcc.Name,"PipeSch") = PipeSch
		' For debugging:
		 MessageBox.Show(oOcc.Name & " was updated." & vbLf &
		 "Schedule set to: " & iProperties.Value(oOcc.Name,"Project","Description"), "Debug Message - Pipe_Sch")
		
	Catch
		MsgBox(oOcc.Name & vbLf & "Parameter 'Sch' not found. Proceeding to next part...")
	
	End Try
Next
Accepted solutions (1)
8,885 Views
9 Replies
Replies (9)
Message 2 of 10

MechMachineMan
Advisor
Advisor

See if switching they syntax of your params helps;

 

oOcc.Definition.Parameters.Item("Sch").Value()

Or 

oOcc.Definition.Parameters.Item("Sch").Expression

 

 

Also,

 

The less sloppy way of accessing iProperties would be:

 

oOcc.Definition.Document.PropertySets.Item("Design Tracking Properties").Item("Description").Value

 

 

 

 


--------------------------------------
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 3 of 10

Anonymous
Not applicable

Thanks for the tips, Mech. I tried both of your parameter access methods. The .Expression modifier doesn't seem to work - I don't get any error messages, but the models don't update. The .Value() modifier does the exact same thing as was happening before where I get the Catch debug message, but then everything still updates as expected anyway.

 

Hmm.. This seemed like a fairly straight-forward bit of code. I guess it's never as simple as you first think it is!

 

P.S. that makes sense about the iProperties - To access the document before telling it to look for a parameter. I'm guessing that's the idea anyway. Although it is a little more lengthy.

0 Likes
Message 4 of 10

Anonymous
Not applicable

Hey there,

 

Looking at your sample part, you're missing the PipeSch parameter.

 

Your code ran fine in my testing.

 

0 Likes
Message 5 of 10

MechMachineMan
Advisor
Advisor
Yeah; it works because you are specifying the document rather than hoping that the snippet is accessing the document you intended; it starts causing errors if you run rules that modify iProperties of invisible documents... Which is what specifying the document clears up.

--------------------------------------
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 6 of 10

Anonymous
Not applicable

Yes, you are correct, rsmabery. The elbow doesn't contain the parameter "PipeSch", but for my flange parts, I used "PipeSch" rather than "Sch". So I just wrote my code to look for both those parameter names. I may go back and update my flanges later though so all are sharing the same parameter names. 

 

So the code is working fine for you? That is weird. I wonder why mine is acting funny. Maybe I just need to restart the app? I'll give that a try.

0 Likes
Message 7 of 10

Anonymous
Not applicable

Try separating the two parameter lines into two separate Try/Catch Blocks.  I didn't realize you didn't have the same parameter in all parts.

Message 8 of 10

MechMachineMan
Advisor
Advisor
Accepted solution

 


APatterson_1 wrote:

I'm using the rule below at the assembly level to go through all parts in the assembly and change the schedule to whatever is set at the assembly level. I used a Try/Catch statement since not all parts will have the "Sch" parameter - only piping and fittings will have that parameter. Maybe I am not using this statement correctly though? I have message boxes for debugging purposes on both the Try and Catch. When I run the rule, I get the Catch message box for all parts as if it couldn't find that Parameter, but then the parts update as expected anyway! Note, the "Sch" parameter at both the assembly and part levels are multivalue text. Could that have something to do with it?

 

See code below and I've also attached a sample part, so feel free to try it out for yourself. As always, any help is much appreciated!

' Set a reference to the assembly component definintion
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

' Iterate through all occurrences
Dim oOcc As ComponentOccurrence
For Each oOcc In oAsmCompDef.Occurrences
	Try
		Parameter(oOcc.Name,"Sch") = PipeSch
		Parameter(oOcc.Name,"PipeSch") = PipeSch
		' For debugging:
		 MessageBox.Show(oOcc.Name & " was updated." & vbLf &
		 "Schedule set to: " & iProperties.Value(oOcc.Name,"Project","Description"), "Debug Message - Pipe_Sch")
		
	Catch
		MsgBox(oOcc.Name & vbLf & "Parameter 'Sch' not found. Proceeding to next part...")
	
	End Try
Next
Also, looking through your code again, I think the issue is that when you use a try loop, it still PERFORMS all of the actions up until the point where it throws the error in the loop.
ie; if it has sch but no pipesch it will still change the sch parameter.
I would try a simpler check before you run the changing; ie;

 

 oCheck = oParam.Item("Sch").Name  'will throw an error if it does not exist. (This is referring to the syntax i talked about in my other post)

Also, double check your message box syntax as it seems your are using 2 different versions... not sure if that will actually cause any issues, but it doesn't hurt to keep it consistent.

 

 

 


--------------------------------------
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 9 of 10

Anonymous
Not applicable

Aaaahhhh.... I just had a face-palm moment. That makes perfect sense now that if even though the "Sch" parameter is there, it will still give me the Catch error handling if the "PipeSch" is not there. Yep, now it works fine! The importance of consistancy is becoming all to real lol. I will definitely go back and update my other parts' parameter names.

 

This is what happens when I take vacation days and immediately jump into coding when I return....

 

Thanks for the help, guys!

0 Likes
Message 10 of 10

Daan_M
Collaborator
Collaborator

-

0 Likes