New Substitute>Derive Assembly>Bounding Box (.ipt only) - iLogic please.

New Substitute>Derive Assembly>Bounding Box (.ipt only) - iLogic please.

andrew_canfield
Collaborator Collaborator
2,721 Views
32 Replies
Message 1 of 33

New Substitute>Derive Assembly>Bounding Box (.ipt only) - iLogic please.

andrew_canfield
Collaborator
Collaborator

 

Hello

Going to search the forum to find something which will copy the workflow below - the result will be a simplified part  made up of Bounding Boxes. not too sure how the options are set using ilogic.

 

 

Substitue Derived Part.JPG

 

Regards

 

Andrew

0 Likes
Accepted solutions (4)
2,722 Views
32 Replies
Replies (32)
Message 21 of 33

andrew_canfield
Collaborator
Collaborator

It's brilliant - thankyou.

I'm going to attempt a 'phase2' now (software development creep) :

  1. break the link of the derived part
  2. save the file path of the parent in the derived part (custom parameter)

The simplified derived parts are going to replace sub assemblies in a GA but I need to know if the original is modified (because the link will have been broken). At the GA level read the filepath of the sub assemblies from the parameter & check if it's newer than when the simplified part was created & then message box - update on file 'xyz' rqd.

Why? because the data management tool isn't Vault & tracks every file within a shrinkwrap - very slow on large assemblies.

Thanks Again

Regards

 

Andrew

0 Likes
Message 22 of 33

andrew_canfield
Collaborator
Collaborator

Hello - I thought I might be able to figure this out but...

Can the work planes be filtered out - they are appearing in the derived part?

Can the link in the derived part be broken (when it's created)?

Regards

Andrew

0 Likes
Message 23 of 33

J-Camper
Advisor
Advisor

@andrew_canfield

 

part of your request is simple.  There is an option to break the link of a DerivedAssemblyComponent.  The other request about part level work planes should be easy, but it seems the DerivedAssemblyDefinition we are building for the new part has a method IncludeAllWorkFeatures, but nothing to Exclude them.  You should be able to change visibility of the work planes off once they are in the new part.  You should be able to replace 1 line of the code with new lines as shown below:

'Existing line:
Call NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)

'Replacement lines:
Dim derivedAsmComp As DerivedAssemblyComponent = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)

For Each oWorkPlane As WorkPlane In NewDoc.ComponentDefinition.WorkPlanes
	oWorkPlane.Visible = False
Next

derivedAsmComp.BreakLinkToFile()

Let me know if you have issues implementing these new lines, or if you truly need them excluded from the DerivedAssemblyDefinition and we can try to find out how.

Message 24 of 33

andrew_canfield
Collaborator
Collaborator

Fantastic - I'd pasted in the wrong code at first but it's looking good - still testing.

 

Thanks Again.

 

Regards

 

Andrew

0 Likes
Message 25 of 33

andrew_canfield
Collaborator
Collaborator

trying to hide work axis & sketches to..working for axis - error on sketches..

	'Call NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)
'Replacement lines:
Dim derivedAsmComp As DerivedAssemblyComponent = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)

For Each oWorkPlane As WorkPlane In NewDoc.ComponentDefinition.WorkPlanes
	oWorkPlane.Visible = False
	
Next
For Each oWorkAxis As WorkAxis In NewDoc.ComponentDefinition.WorkAxes

	oWorkAxis.Visible = False
Next



    For Each o2DSketch As Sketch In doc.ComponentDefinition.Sketches
        o2DSketch.Visible = False
    Next

    For Each o3DSketch As Sketch3D In doc.ComponentDefinition.Sketches3D
        o3DSketch.Visible = False
    Next

derivedAsmComp.BreakLinkToFile()

 

 

Error on Line 63 : 'doc' is not declared. It may be inaccessible due to its protection level.
Error on Line 67 : 'doc' is not declared. It may be inaccessible due to its protection level.

 

Regards

 

Andrew

0 Likes
Message 26 of 33

J-Camper
Advisor
Advisor

you have an undefined object "doc" that you are trying to use:

temp_pic.JPG

Message 27 of 33

andrew_canfield
Collaborator
Collaborator

All good thanks.

 

The joy of cut & paste! - would it be possible to delete these feature in the new part?

For Each o2DSketch As Sketch In NewDoc.ComponentDefinition.Sketches
o2DSketch.Visible = False     {replace false with delete doesn't work}
Next

 

Regards

 

Andrew

0 Likes
Message 28 of 33

J-Camper
Advisor
Advisor

Well you can't do that as a user, so I doubt the API can.  Ideally they would not come over at all, I was just giving a quick band-aid fix to the issue that there is not a straight forward simple method to Exclude all work features in the DerivedAssemblyDefiniton, as they have for including them.  If they are an issue being there at all, even when set to not visible, then we need to figure out how to stop them from coming over to begin with.

 

Let me ask you this, Do these parts get derived into other parts at any point?  We might be able to change the part properties of the features so they are not marked for export before including them in the definition.  I'm not sure if they would need to be saved before the change takes affect.  I'm asking before doing because any part deriving these would want to be updated to re-mark the intended features for export.  If they aren't generally derived for reasons other than the process, then it shouldn't be any issue other than read/write privileges of the files themselves [if i do end up needing to save the files before the inclusion step]

0 Likes
Message 29 of 33

J-Camper
Advisor
Advisor

@andrew_canfield,

 

I was looking into restricting the Part Level sketches, and I think I have it working as long as you don't want any of them to show up. I'm setting the ObjectVisibility Property of the Assembly document before creating the new part.  For what ever reason, as long as they aren't visible at the time, they don't show up in the derived part.  It doesn't hold the ObjectVisibility toggle if you are taking the master view, but it should work with any user created view representation.

 

Full code:

Sub Main
	If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule is designed to only work in assembly documents.", "Wrong Document Type") : Exit Sub
	'Main Setup:
	Dim AsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim FileName As String = AsmDoc.FullFileName : FileName = Left(FileName, (FileName.Length-4)) & "_SUB" & ".ipt"
	
	'ViewRep Sample Setup:
	Dim RepID As String = ""
	Dim RepIDlist As New List(Of String)
	'Get List from file so we can choose view from list. This Can be automated with desired name
	For Each View In AsmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations
		RepIDlist.Add(View.Name)
	Next	

	'Temporarily choosing view from list. This can be automated with desired name
	RepID = InputListBox("Select a View Rep to derive", RepIDlist, RepIDlist.Item(0), Title := "View Representations", ListName := "Available:")
	If RepID = "" Then RepID = "Master" 'Setting a Default Value for nothing selected
	
	'Set View representation so visibility can be verified
	Dim visRep As DesignViewRepresentation = AsmDoc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.Item(RepID)
	visRep.Activate
	
	Dim ReturnState As Boolean = visRep.Locked
	
	If visRep.DesignViewType = DesignViewTypeEnum.kMasterDesignViewType
		ReturnState = False
		MessageBox.Show("ObjectVisibility Toggles don't affect Derived Parts using the ""Master"" View Representation", "Limited Functionality")
	End If
	
	If ReturnState Then visRep.Locked = Not(ReturnState)
	
	Call ObjectVisibility(AsmDoc, False)
	
	'New Document Setup:
	Dim NewDoc As PartDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , False) 'include path to template file [if defualt is insufficient] between ,  , after DocumentTypeEnum.kPartDocumentObject
	Dim oDerivedAsmDef As DerivedAssemblyDefinition = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AsmDoc.FullFileName)

	'Derived Assembly Options:
	oDerivedAsmDef.ScaleFactor = 1
	oDerivedAsmDef.ReducedMemoryMode = True
	oDerivedAsmDef.UseColorOverridesFromSource = True
	oDerivedAsmDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
	oDerivedAsmDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
	oDerivedAsmDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
	oDerivedAsmDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
	oDerivedAsmDef.DeriveStyle =  DerivedComponentStyleEnum.kDeriveAsSingleBodyNoSeams
	oDerivedAsmDef.InclusionOption = DerivedComponentOptionEnum.kDerivedExcludeAll
	oDerivedAsmDef.ActiveDesignViewRepresentation = RepID 'Where RepID is a string of the View rep name
	
	'Limit bounding box to part files only
	Dim partCol As ObjectCollection = GetOnlyParts(oDerivedAsmDef.Occurrences)
	
	'Limit bounding box to smaller than target
	Dim minLimit As Double = 20 'cm [internal units are cm]
	Dim RefinedpartCol As ObjectCollection =RefinePartsMin(partCol, minLimit)
	
	For Each Oc As DerivedAssemblyOccurrence In RefinedpartCol
		Oc.InclusionOption = DerivedComponentOptionEnum.kDerivedBoundingBox
	Next
	
	Dim derivedAsmComp As DerivedAssemblyComponent = NewDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)
	derivedAsmComp.BreakLinkToFile()
	
	'Save New Document
	Call SaveFile(NewDoc, FileName)
	
	'Open new document
	ThisApplication.Documents.Open(FileName, True)
	
	'Activate original assembly
	AsmDoc.Activate
	
	'Call Toggle Sub
	Call ObjectVisibility(AsmDoc, True)
	
	If ReturnState Then visRep.Locked = ReturnState
	
End Sub

Sub ObjectVisibility(AsmDoc As AssemblyDocument, oToggle As Boolean)

	Dim oVis As ObjectVisibility = AsmDoc.ObjectVisibility
	oVis.Sketches = oToggle
	oVis.Sketches3D = oToggle
	oVis.UserWorkPlanes = oToggle
	oVis.UserWorkAxes = oToggle
	oVis.UserWorkPoints = oToggle
	oVis.OriginWorkPlanes =oToggle
	oVis.OriginWorkAxes = oToggle
	oVis.OriginWorkPoints = oToggle

End Sub

Sub SaveFile(oDoc As Document, NewFileName As String)
	
	Try
		oDoc.SaveAs(NewFileName, False)
	Catch
		MessageBox.Show(oDoc.DisplayName & " cannot be overwritten.", "Terminating")
		oDoc.Close(True)
		Exit Sub
	End Try

End Sub

Function RefinePartsMin(mainCol As ObjectCollection, limit As Double) As ObjectCollection
	Dim Result As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	
	For Each Oc As DerivedAssemblyOccurrence In mainCol
		Dim oBox As Box = Oc.ReferencedOccurrence.RangeBox
		Dim diagonal As Vector = oBox.MinPoint.VectorTo(oBox.MaxPoint)
		If diagonal.Length <= limit Then Result.Add(Oc)
	Next
	
	Return Result
End Function

Function GetOnlyParts(mainCol As DerivedAssemblyOccurrences) As ObjectCollection
	Dim Result As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
	
	For Each Oc As DerivedAssemblyOccurrence In mainCol
		
		If Oc.IsAssembly = False
			
			If Oc.ReferencedOccurrence.Suppressed = False And Oc.ReferencedOccurrence.Visible = True
				Result.Add(Oc)
			End If
		
		Else
			
			For Each OcSub As DerivedAssemblyOccurrence In GetOnlyParts(Oc.SubOccurrences)
				If OcSub.ReferencedOccurrence.Suppressed = False And OcSub.ReferencedOccurrence.Visible = True
					Result.Add(OcSub)
				End If
			Next
		
		End If
	Next
	
	Return Result
End Function

 

Let me know if you have any questions, or if this is not working as intended.

0 Likes
Message 30 of 33

andrew_canfield
Collaborator
Collaborator

Hello & thanks again,

 

The previous code is ok, this one has include the sketch used by the frame generator & larger items have been omitted.

 

Something I am thinking about is trying to run the code as an external rule or in a sequence:

Open file path

Run rule

save & close

Open next filepath in list...

 

I think I have something like this from last year -  but on a newer PC now the old HD packed up.

 

Regards

 

Andrew

0 Likes
Message 31 of 33

andrew_canfield
Collaborator
Collaborator

Morning

I have a slightly different issue (of my own making) & it involves trying to run this rule with another rule in the same assembly. The problem is in saving the newly created part - the first rule uses 'component replace' & reads the filename & path from an excel table then 'shared variable' is used to note the name of the new file.

When running the 'Derive big but block out small' code the shared variable can be displayed in a message box but when trying to save this appears:

NameSave.PNG

it's seeing 'Part5' but I can't figure out how to change this.. 'NewPath' is displayed correctly..

 

	'Save New Document
	
	Dim FilePath3 As String = SharedVariable("FilePath2")
	Dim FileName3 As String = SharedVariable("FileName2")
	
	NewPath = FilePath3 & FileName3 & "_SUB" & ".ipt"
	MessageBox.Show(NewPath, "3")
	Call SaveFile(NewDoc, FileName)
	
	
	'Open new document
	'ThisApplication.Documents.Open(FileName, True)

 

Sub SaveFile(oDoc As Document, NewFileName As String)
	
	Try
		oDoc.SaveAs(NewPath, False)
	Catch
		MessageBox.Show(oDoc.DisplayName & " cannot be overwritten.", "Terminating")
		oDoc.Close(True)
		Exit Sub
	End Try

End Sub

'Replace' is the ilogic rule which swaps out the models - after swapping 'run other' is used together with the shared variable to pick up the name of the new model.

 

I have a test assembly built  & can upload if it will help.

 

Regards

 

Andrew

0 Likes
Message 32 of 33

J-Camper
Advisor
Advisor
Accepted solution

@andrew_canfield,

 

Sorry for the late reply. A test assembly would be helpful.  iLogic can be finicky when you start calling rules from rules and changing the active document between those rules. 

Message 33 of 33

andrew_canfield
Collaborator
Collaborator

Thanks again - I'd added a variable & not changed another - all good 🙂 

0 Likes