Search all derived parts inside an assembly, open each of them and do something and close again

Search all derived parts inside an assembly, open each of them and do something and close again

Charlies_3D_T
Advocate Advocate
1,802 Views
30 Replies
Message 1 of 31

Search all derived parts inside an assembly, open each of them and do something and close again

Charlies_3D_T
Advocate
Advocate

Hello,

 

I am not able to get a working rule for the following:

 

I want to scan an assembly and all it's sub assemblies to see how many derived parts there are. After that i want to open eacht of them get a message with info and close the open one and open the next. 

 

Can someon of you guys give me a hint or a beginning for this? Because i keep struggling and don't get it working. 

 

Thank you!

 

0 Likes
Accepted solutions (3)
1,803 Views
30 Replies
Replies (30)
Message 21 of 31

J-Camper
Advisor
Advisor

So the issue was that I was not aware this rule would be set to an event trigger for saving, so I was performing saves within the rule to make sure our changes stuck.  I reworked it slightly to get rid of unnecessary saves [only unnecessary because save follows end of rule, with that event trigger]

 

I tried to set the rule up to remain as an active occurrence, but when the flat pattern gets created from within the ActiveEdit occurrence, it switches to fully opening the document it self.  This just means that any documents getting a flat pattern created will get opened, and remain open until you close them.  When I try to close them within the running rule I get a save error. 

 

The attached rule will function whether you save from Part or assembly, but if the part needs a new flat pattern, it will get opened and stay opened. 

 

Let me know if you are having any issues or have any questions

0 Likes
Message 22 of 31

Charlies_3D_T
Advocate
Advocate

Hello,

 

The rule is working almost perfect.

 

But when i try it as following:

 

I have the assembly i change some parameters so a sm and a derived sm change and i save the assembly the paramters are not transfered to the derived sm part. I need to say that the derived sm has already a flat pattern. So if i delete the flat pattern and run the rule then i get the parameters from the base part. I think we just need to change that it also transfers the parameters if a flat pattern already exists? 

 

0 Likes
Message 23 of 31

J-Camper
Advisor
Advisor
Accepted solution

I think we just need to update the Part in order to pick up the derived part changes. 

 

Add the Update line near the top of the SM_Bundle sub:

Sub SM_Bundle(PassDoc As PartDocument)
	'local sheet metal part rule.  No document type or subtype checking checking	
	Dim oDoc As PartDocument = PassDoc
	Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
	
	'Update to pick up derived part changes
	oDoc.Update

 

I think that should be all you need, but let me know if you are still having issues.

0 Likes
Message 24 of 31

Charlies_3D_T
Advocate
Advocate

@J-Camper 

 

Thank you i think it is working!

 

One strange thing the unfold lenght is now in cm and not in mm and is rounded. For example i have a unfold with a length of 199 and i get 20 in my values. 

 

I can not find the position where we declare this. 

0 Likes
Message 25 of 31

_dscholtes_
Advocate
Advocate
Accepted solution

Inventor internal units are: kg (mass), cm (length), sec (time), kelvin (temp) and rad (angle). Your 199 (mm) is returned as 20 (cm). The easiest way to deal with this is just to multiply your dimension by 10 (e.g. <something> * 10).
Or you could use Inventor's UnitsOfMeasure object to convert between Inventors internal units and any other units .


0 Likes
Message 26 of 31

Charlies_3D_T
Advocate
Advocate

@J-Camper 

 

I have found a problem with the code. Now when i work with a master part and use derived parts it looks at the master part. Is it possible for us to check if the ref part is a sheet metal or a normal part? And if normal ignore and run the sheetmetal hole from the active part? 

 

		
		'I'm exiting here. if you have sheet metal parts with derived parts that are not Sheet Metal, this would be an issue
		If IsNothing(ParentDef) Then Logger.Debug("No Derived Part was a Sheet Metal Part") : Exit Sub
		'Otherwise it will be fine
		
		Call SheetMetal_Hole(ParentDef.Document, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on parent document")

        Else
			
		Logger.Debug("DerivedPartComponents Count <= 0")
		
		Call SheetMetal_Hole(oDoc, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on open document")
			
	End If
	
	Call Events(oDoc)

 

0 Likes
Message 27 of 31

J-Camper
Advisor
Advisor

It has been awhile since I've looked at this, but I think the snippet you pulled out is all that will need to change.  In the snippet you can see my comment that I was canceling the sub if none of the derived components were Sheetmetal parts.  We can handle that situation differently if that is what you want.

 

Try replacing that section with this:

		
		'I'm running the Passed sheet metal document by itself now since none of the derived components were sheetmetal parts
		If IsNothing(ParentDef) 
			Logger.Debug("No Derived Part was a Sheet Metal Part") 
			Call SheetMetal_Hole(oDoc, oDoc) 
			Logger.Debug("Completed SheetMetal_Hole on open document")
			Call Events(oDoc)
			Exit Sub
		End If
		
		Call SheetMetal_Hole(ParentDef.Document, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on parent document")

        Else
			
		Logger.Debug("DerivedPartComponents Count <= 0")
		
		Call SheetMetal_Hole(oDoc, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on open document")
			
	End If
	
	Call Events(oDoc)

 

Let me know how this goes, or if you have any questions.

0 Likes
Message 28 of 31

Charlies_3D_T
Advocate
Advocate

@J-Camper 

 

I get an error now. I get an error on the Check

 

Check For derived part parent To Get features
	If oCompDef.ReferenceComponents.DerivedPartComponents.Count > 0
		Logger.Debug("DerivedPartComponents Count > 0")

 

This is how i have the code:

 

Sub SM_Bundle(PassDoc As PartDocument)
		'local sheet metal part rule.  No document type or subtype checking checking	
	Dim oDoc As PartDocument = PassDoc
	Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
	
	'Update to pick up derived part changes
	oDoc.Update

	'define the property set
	Dim userParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters

	Try
	    BendRadius_Parm = oDoc.ComponentDefinition.Parameters("BendRadius_Ilogic")
	Catch
	    Dim newParam As UserParameter = userParams.AddByExpression("BendRadius_Ilogic", 1, "mm")
	End Try
	
	'Set Object to collect values to names
	Dim PropList As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	PropList.Add("Lengte", 20.5)
	PropList.Add("Breedte", 10.5)
	PropList.Add("Thickness", 1.5)
	PropList.Add("Profile", "Empty String")
	Logger.Debug("Setup NameValueMap 1")
	
	'Fill values based on whether or not there is a derived part
	If oCompDef.HasFlatPattern = True
		
		Logger.Debug("HasFlatPattern")
		
		PropList.Value("Lengte") = (Round(oCompDef.FlatPattern.Length*10,0)) 
		PropList.Value("Breedte") = (Round(oCompDef.FlatPattern.Width*10, 0))
		PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value*10
		PropList.Value("Profile") = "SM " & PropList.Value("Lengte") & "x" & PropList.Value("Breedte") & "x" & PropList.Value("Thickness")
		
		Logger.Debug("existing FlatPattern Values: " & PropList.Value("Profile"))
		
	Else
		Logger.Debug("Not HasFlatPattern")
		
		oCompDef.Unfold
		Logger.Debug("Open Part is Unfolded")
		oDoc.Update
		Logger.Debug("Open Part is Updated")
		'oDoc.Save
		'Logger.Debug("Open Part is Saved")
		
		PropList.Value("Lengte") = (Round(oCompDef.FlatPattern.Length*10, 0))'FindMax(oCompDef.RangeBox, "L") 
		PropList.Value("Breedte") = (Round(oCompDef.FlatPattern.Width*10, 0))'FindMax(oCompDef.RangeBox, "W") 
		PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value*10
		PropList.Value("Profile") = "SM " & PropList.Value("Lengte") & "x" & PropList.Value("Breedte") & "x" & PropList.Value("Thickness")
		Logger.Debug("local-new FlatPattern Values: " & PropList.Value("Profile"))
		
		oCompDef.FlatPattern.ExitEdit
		Logger.Debug("Open Part is Refolded")
			
	End If
	
	'Set iProperties
	Dim CustProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
	
	For i = 1 To PropList.Count
		Try 
			CustProps.Item(PropList.Name(i)).Expression = PropList.Value(PropList.Name(i))
		Catch
			CustProps.Add(PropList.Value(PropList.Name(i)), PropList.Name(i))
		End Try
	Next
	
	Check For derived part parent To Get features
	If oCompDef.ReferenceComponents.DerivedPartComponents.Count > 0
		Logger.Debug("DerivedPartComponents Count > 0")
		
		Dim ParentDef As SheetMetalComponentDefinition = Nothing
		
		For Each RefDoc As Document In oDoc.ReferencedDocuments
			Logger.Debug("DerivedPartComponent Loop")
			If RefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
				Logger.Debug("Derived Comp Is SM")
				ParentDef = RefDoc.ComponentDefinition
				Exit For
			End If
		Next
		
'I'm running the Passed sheet metal document by itself now since none of the derived components were sheetmetal parts
		If IsNothing(ParentDef) 
			Logger.Debug("No Derived Part was a Sheet Metal Part") 
			Call SheetMetal_Hole(oDoc, oDoc) 
			Logger.Debug("Completed SheetMetal_Hole on open document")
			Call Events(oDoc)
			Exit Sub
		End If
		
		Call SheetMetal_Hole(ParentDef.Document, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on parent document")

        Else
			
		Logger.Debug("DerivedPartComponents Count <= 0")
		
		Call SheetMetal_Hole(oDoc, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on open document")
			
	End If
	
	Call Events(oDoc)
		
End Sub

 

0 Likes
Message 29 of 31

J-Camper
Advisor
Advisor

can you run the rule in Log Level: Debug  and post the results.  You can copy and paste from the iLogic Log Window.

0 Likes
Message 30 of 31

Charlies_3D_T
Advocate
Advocate

@J-Camper 

 

Rule Compile Errors in Metaalcomfort - FlatPattern, in _GP_Metalen_Trede_1.ipt

Error on Line 95 : Check is niet gedeclareerd. Het item is mogelijk niet toegankelijk als gevolg van het bijbehorende beveiligingsniveau.
Error on Line 95 : Expressie wordt verwacht.
Error on Line 95 : Methodeargumenten moeten tussen haakjes worden geplaatst.

 

Warning on Line 189 : Functie GetiLogicEventPropSet retourneert niet voor alle codepaden een waarde. Als het resultaat wordt gebruikt, kan een uitzondering voor een null-verwijzing optreden tijdens runtime.

 

It's dutch... 

 

 

0 Likes
Message 31 of 31

J-Camper
Advisor
Advisor
Accepted solution

Nevermind I  found the issue.  This line is supposed to be a comment:

Check For derived part parent To Get features
Should be:
'Check For derived part parent To Get features

That should be all you need, but here is the SM Bundle Sub:

Sub SM_Bundle(PassDoc As PartDocument)
		'local sheet metal part rule.  No document type or subtype checking checking	
	Dim oDoc As PartDocument = PassDoc
	Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
	
	'Update to pick up derived part changes
	oDoc.Update

	'define the property set
	Dim userParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters

	Try
	    BendRadius_Parm = oDoc.ComponentDefinition.Parameters("BendRadius_Ilogic")
	Catch
	    Dim newParam As UserParameter = userParams.AddByExpression("BendRadius_Ilogic", 1, "mm")
	End Try
	
	'Set Object to collect values to names
	Dim PropList As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
	PropList.Add("Lengte", 20.5)
	PropList.Add("Breedte", 10.5)
	PropList.Add("Thickness", 1.5)
	PropList.Add("Profile", "Empty String")
	Logger.Debug("Setup NameValueMap 1")
	
	'Fill values based on whether or not there is a derived part
	If oCompDef.HasFlatPattern = True
		
		Logger.Debug("HasFlatPattern")
		
		PropList.Value("Lengte") = (Round(oCompDef.FlatPattern.Length*10,0)) 
		PropList.Value("Breedte") = (Round(oCompDef.FlatPattern.Width*10, 0))
		PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value*10
		PropList.Value("Profile") = "SM " & PropList.Value("Lengte") & "x" & PropList.Value("Breedte") & "x" & PropList.Value("Thickness")
		
		Logger.Debug("existing FlatPattern Values: " & PropList.Value("Profile"))
		
	Else
		Logger.Debug("Not HasFlatPattern")
		
		oCompDef.Unfold
		Logger.Debug("Open Part is Unfolded")
		oDoc.Update
		Logger.Debug("Open Part is Updated")
		'oDoc.Save
		'Logger.Debug("Open Part is Saved")
		
		PropList.Value("Lengte") = (Round(oCompDef.FlatPattern.Length*10, 0))'FindMax(oCompDef.RangeBox, "L") 
		PropList.Value("Breedte") = (Round(oCompDef.FlatPattern.Width*10, 0))'FindMax(oCompDef.RangeBox, "W") 
		PropList.Value("Thickness") = oCompDef.Parameters("Thickness").Value*10
		PropList.Value("Profile") = "SM " & PropList.Value("Lengte") & "x" & PropList.Value("Breedte") & "x" & PropList.Value("Thickness")
		Logger.Debug("local-new FlatPattern Values: " & PropList.Value("Profile"))
		
		oCompDef.FlatPattern.ExitEdit
		Logger.Debug("Open Part is Refolded")
			
	End If
	
	'Set iProperties
	Dim CustProps As PropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
	
	For i = 1 To PropList.Count
		Try 
			CustProps.Item(PropList.Name(i)).Expression = PropList.Value(PropList.Name(i))
		Catch
			CustProps.Add(PropList.Value(PropList.Name(i)), PropList.Name(i))
		End Try
	Next
	
	'Check For derived part parent To Get features
	If oCompDef.ReferenceComponents.DerivedPartComponents.Count > 0
		Logger.Debug("DerivedPartComponents Count > 0")
		
		Dim ParentDef As SheetMetalComponentDefinition = Nothing
		
		For Each RefDoc As Document In oDoc.ReferencedDocuments
			Logger.Debug("DerivedPartComponent Loop")
			If RefDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
				Logger.Debug("Derived Comp Is SM")
				ParentDef = RefDoc.ComponentDefinition
				Exit For
			End If
		Next
		
'I'm running the Passed sheet metal document by itself now since none of the derived components were sheetmetal parts
		If IsNothing(ParentDef) 
			Logger.Debug("No Derived Part was a Sheet Metal Part") 
			Call SheetMetal_Hole(oDoc, oDoc) 
			Logger.Debug("Completed SheetMetal_Hole on open document")
			Call Events(oDoc)
			Exit Sub
		End If
		
		Call SheetMetal_Hole(ParentDef.Document, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on parent document")

        Else
			
		Logger.Debug("DerivedPartComponents Count <= 0")
		
		Call SheetMetal_Hole(oDoc, oDoc)
		
		Logger.Debug("Completed SheetMetal_Hole on open document")
			
	End If
	
	Call Events(oDoc)
		
End Sub

 

Let me know how that goes

0 Likes