iLogic Part contains Solidbody

iLogic Part contains Solidbody

dusan.naus.trz
Advisor Advisor
1,319 Views
16 Replies
Message 1 of 17

iLogic Part contains Solidbody

dusan.naus.trz
Advisor
Advisor

Hello,
I've been thinking about how to do that for a long time.
I have a code that unfolds all the Sheet Metal. It works.
The problem is when I use it on Components that contain Solidbody. Pop up error, unfold, contain only some ipt

Video and File in attachment.

I want to write this down, but I don't know how.

If Part contain more than 1 Solidbody Then Continue For

 

Dim doc As Document = ThisApplication.ActiveDocument
'Loop through all referenced docs in assembly
	
	For Each oDoc As Document In doc.AllReferencedDocuments
		'Check if unfold exists. If not try to unfold
		
		If oDoc.ComponentDefinition.Type = 150995200 Then
			Dim oCompDefSM As SheetMetalComponentDefinition
			oCompDefSM = oDoc.ComponentDefinition
			    
			If oCompDefSM.HasFlatPattern = False Then
				oCompDefSM.Unfold
				oCompDefSM.FlatPattern.ExitEdit
			    oCompDefSM.Document.Close (True)
			End If
		End If
	Next

 

0 Likes
Accepted solutions (1)
1,320 Views
16 Replies
Replies (16)
Message 2 of 17

JelteDeJong
Mentor
Mentor
Accepted solution

you can try this rule.

Dim doc As Document = ThisApplication.ActiveDocument
'Loop through all referenced docs in assembly

For Each oDoc As Document In doc.AllReferencedDocuments
    ' Check if its a part document 
    If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
        Dim pDoc As PartDocument = oDoc
        ' Check if there are multiple solid bodies
        If (pDoc.ComponentDefinition.HasMultipleSolidBodies = False) Then
            ' Check if its a sheetmetal part
            If (pDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
                Dim oCompDefSM As SheetMetalComponentDefinition = oDoc.ComponentDefinition
                'Check if unfold exists. If not try to unfold
                If oCompDefSM.HasFlatPattern = False Then
                    oCompDefSM.Unfold()
                    oCompDefSM.FlatPattern.ExitEdit()
                    oCompDefSM.Document.Close(True)
                End If
            End If
        End If
    End If
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 3 of 17

dusan.naus.trz
Advisor
Advisor

Hello,
thank you very much. You are great. Everything works.

Message 4 of 17

ToniCoelhoE8VJF
Participant
Participant

@JelteDeJong This Code is brilliant!  

 

I have one request. Can we code it so that we can chose the A-side every time it unfolds a part in the assembly?

 

We need this because we Fold Galvanized panels with Bottom face having rough finish, which give us control on the overall look of the final assembly. And also for all the other materials we opt for majority of folds upwards from the A-side.

 

Hence a human input is required when the A-side is chosen.

0 Likes
Message 5 of 17

WCrihfield
Mentor
Mentor

Hi @ToniCoelhoE8VJF.  The good news is yes, I think that is possible.  You would have to switch from using the regular SheetMetalComponentDefinition.Unfold method, to using the SheetMetalComponentDefinition.Unfold2 method, which asks for the Face object to use as the FlatPattern.BaseFace.  You would most likely have to also include the 'Pick' function, to pause the code for manual, mouse selection of a Face for it to use there.  But before calling that function, you would have to 'visibly' open each sheet metal part type document, because they are just being referenced in the background while another document is active in the example above.  This would be needed so that you can see it on your screen before picking one of its faces, because that earlier code example was just doing all that processing in the background.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 17

ToniCoelhoE8VJF
Participant
Participant

@WCrihfield @JelteDeJong Can the above not be incorporated in the above code just so that it does all the tasks in the background, like opening the file>giving the popup to Pick the base face>closing the file> going onto the next sheetmetal part?

 

Apologies, I am not fully iLogic Savy yet but building my expertise pas a pas. 

0 Likes
Message 7 of 17

WCrihfield
Mentor
Mentor

Yes.  The code can visibly open each sheet metal part as it comes to them, then prompt the user to manually click the mouse on a face of that part to use as the 'base face', then proceed to do the unfolding, and move on to the next part.  Or, if you had those specific faces 'named', then no manual user interactions would be needed at run time, and the code could just find the face by its name, and use that, without needing to visibly open each one, and without needing to prompt the user to pick a face for each one.  That named face route would require preparation ahead of time though.  When in a part, we can select a face, right-click on it, and choose 'Assign Name...' from the context menu, then assign a name to it.  That creates an attribute on that face that we can find by code later.  That works for faces, edges, and vertex objects.

Below is an edited copy of Jelte's code example.  I added a line of code for visibly opening the sheet metal part only if it does not already have a flat pattern, and needs to be unfolded.  Then, I added a line of code to prompt the user to select a 'base face' for the unfold.  If something was picked, it will then try to unfold the part using that picked face.  Then it will try to visibly close that document.  Since this document is being referenced by the originally active document, it will not be fully closed, just visibly closed.  I have not tested this edited code, so proceed with caution.

Dim doc As Document = ThisApplication.ActiveDocument
'Loop through all referenced docs in assembly

For Each oDoc As Document In doc.AllReferencedDocuments
	' Check if its a part document 
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Dim pDoc As PartDocument = oDoc
		' Check if there are multiple solid bodies
		If (pDoc.ComponentDefinition.HasMultipleSolidBodies = False) Then
			' Check if its a sheetmetal part
			If (pDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
				Dim oCompDefSM As SheetMetalComponentDefinition = oDoc.ComponentDefinition
				'Check if unfold exists. If not try to unfold
				If oCompDefSM.HasFlatPattern = False Then
					'<<< VISIBLY OPENING THIS REFERENCED PART NOW >>>
					oDoc = ThisApplication.Documents.Open(oDoc.FullDocumentName, True)
					Dim oBaseFace As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick 'Base Face' for Unfold")
					If oBaseFace IsNot Nothing Then
						Try
							oCompDefSM.Unfold2(oBaseFace)
							oCompDefSM.FlatPattern.ExitEdit()
						Catch
							'what to do if that caused an error that got suppressed
						End Try
					End If
					Try
						oDoc.Close(True) 'True = skip save when closing (just closing visibly, not fully)
					Catch
					End Try
				End If
			End If
		End If
	End If
Next 'oDoc

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 8 of 17

ToniCoelhoE8VJF
Participant
Participant

Thank you @WCrihfield.

 

I will now test this code and let you know the outcome. 

 

0 Likes
Message 9 of 17

ToniCoelhoE8VJF
Participant
Participant

 

@WCrihfield I have tested the code please see the above video.

 

The code seems to request a Base Face to be selected even though a A-Face is defined in the part.

 

I made the TEST assembly of 2 parts with one without A-Face being defined and other with A-Face defined.

0 Likes
Message 10 of 17

WCrihfield
Mentor
Mentor

I did not know that you wanted to consider that, since there was nothing about it in the original code, and no mention of it.  The A-Side Face seems to be a different property (FlatPattern.ASideFace) of the FlatPattern than the BaseFace, because it can have a BaseFace without having an A-Side Face.  We can access those before the FlatPattern exists through the SheetMetalComponentDefinition.ASideDefinitions property.  I assume then that after code checks if the part does not have a flat pattern, it should then check for the existence of that A-Side face, before prompting the user to select a face, and use that A-Side Face if it exists instead?  If that is your intention, then we can set things up that way.

Dim doc As Document = ThisApplication.ActiveDocument
'Loop through all referenced docs in assembly

For Each oDoc As Document In doc.AllReferencedDocuments
	' Check if its a part document 
	If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		Dim pDoc As PartDocument = oDoc
		' Check if there are multiple solid bodies
		If (pDoc.ComponentDefinition.HasMultipleSolidBodies = False) Then
			' Check if its a sheetmetal part
			If (pDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}") Then
				Dim oCompDefSM As SheetMetalComponentDefinition = oDoc.ComponentDefinition
				'Check if unfold exists. If not try to unfold
				If oCompDefSM.HasFlatPattern = False Then
					Dim oBaseFace As Face = Nothing
					If oCompDefSM.ASideDefinitions.Count > 0 Then
						oBaseFace = oCompDefSM.ASideDefinitions.Item(1).ASideFace
					Else 'no A-Side Definitions, so have user pick a face
						'<<< VISIBLY OPENING THIS REFERENCED PART NOW >>>
						oDoc = ThisApplication.Documents.Open(oDoc.FullDocumentName, True)
						oBaseFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick 'Base Face' for Unfold")
					End If
					If oBaseFace IsNot Nothing Then
						Try
							oCompDefSM.Unfold2(oBaseFace)
							oCompDefSM.FlatPattern.ExitEdit()
						Catch
							'what to do if that caused an error that got suppressed
						End Try
					End If
					Try
						oDoc.Close(True) 'True = skip save when closing (just closing visibly, not fully)
					Catch
					End Try
				End If
			End If
		End If
	End If
Next 'oDoc

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 12 of 17

ToniCoelhoE8VJF
Participant
Participant

@WCrihfield works like a gem as intended!!

 

It is folding the part with which has the A-face as intended. Can we code it to prompt us to create A-side instead of selecting BaseFace for the part which does not have A-side and then flat pattern it with the A-side created? 

Message 13 of 17

WCrihfield
Mentor
Mentor

Hi @GosponZ.  Looks like a pretty long code process with a lot of separate code routines that took a lot of time to put together.  I will take your word for it that it works well, but I did not test it myself, because I'm a little short on time, and have no need for it.

 

Hi @ToniCoelhoE8VJF.  Sure.  We can break away from the original code layout, and set this up in two code routines, giving the second one a few options for how it works, to make it a bit more dynamic.  A 'Main' code routine that simply deal with getting & supplying the current document and its referenced documents.  And a second routine for doing the unfold task, which lets us specify if we want to use the 'A-Side' definition, if one already exists, whether we want to 'prompt user to pick face, and whether we want to create a new A-SideDefinition using the manually picked face.  Once again, I have not tested this, so let me know if it works OK for you.  If you do not have your iLogic Log tab open, then it would probably be a good idea to show that before running this, then set your iLogic Log level to Trace (not necessarily detailed trace), that way, if it writes anything to the iLogic Log as feedback, it will be captured, and you can look in that tab after the code is done, to see if anything was written into it.  If a sheet metal part did not have a flat pattern, but was not modifiable for some reason, it writes a warning about that, to let you know.  Also, if an error happens while trying to unfold one of them, the error is written to that Log, instead of a MessageBox or Msg, to speed things up.

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	UnFoldSheetMetalPart(oDoc, True, True, True)
	For Each oRefDoc In oDoc.AllReferencedDocuments
		UnFoldSheetMetalPart(oRefDoc, True, True, True)
	Next
	oDoc.Update2(True)
End Sub

Sub UnFoldSheetMetalPart(doc As Inventor.Document, _
	Optional UseASideFaceIfAvailable As Boolean = False, _
	Optional PromptForFace As Boolean = False, _
	Optional CreateASideDefinition As Boolean = False)
	
	If (doc Is Nothing) OrElse (Not TypeOf doc Is PartDocument) Then Return
	Dim oPDoc As PartDocument = doc
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	If oSMDef.HasFlatPattern Then Return
	If Not oPDoc.IsModifiable Then
		Logger.Warn("Following sheet metal part with no FlatPattern was 'Not Modifiable':" _
		& vbCrLf & oPDoc.DisplayName)
		Return
	End If
	Dim oBaseFace As Face = Nothing
	Dim oASideFace As Face = Nothing
	Dim oNewView As Inventor.View = Nothing
	Dim oASideDefs As ASideDefinitions = oSMDef.ASideDefinitions
	If UseASideFaceIfAvailable = True Then
		If oASideDefs.Count > 0 Then
			oASideFace = oASideDefs.Item(1).ASideFace
			oBaseFace = oASideFace
		End If
	End If
	If PromptForFace = True Then
		If oBaseFace Is Nothing Then 
			'make the part visible, and the active document
			If oPDoc.Views.Count = 0 Then
				oNewView = oPDoc.Views.Add()
				oNewView.Activate()
			Else
				oPDoc.Views.Item(1).Activate()
			End If
			'prompt user to pick face
			oBaseFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick 'A-Side Face'")
			If oBaseFace Is Nothing Then Return
		End If
		If CreateASideDefinition = True Then
			oASideFace = oBaseFace
			'add an ASideDefinition for that face
			oASideDefs.Add(oASideFace)
		End If
	End If
	If oBaseFace Is Nothing Then
		Try
			oSMDef.Unfold()
		Catch ex As Exception
			Logger.Error("SheetMetalComponentDefinition.Unfold method error for:" & _
			vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
		End Try
	Else
		Try
			oSMDef.Unfold2(oBaseFace)
		Catch ex As Exception
			Logger.Error("SheetMetalComponentDefinition.Unfold2 method error for:" & _
			vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
		End Try
	End If
	If oNewView IsNot Nothing Then oNewView.Close()
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 14 of 17

GosponZ
Collaborator
Collaborator

I understand you. I'm using daily, work for me. I just place if anybody want. You would probably fast tryout then that much you typed. I tried your rule and i say good rule, but not working for me

0 Likes
Message 15 of 17

WCrihfield
Mentor
Mentor

I do design a lot of sheet metal parts using the sheet metal tools in Inventor, but I honestly rarely ever 'need' to unfold it or create a FlatPattern of it.  We outsource most sheet metal part fabrication that requires complex machining &/or bending to other local companies.  Since different fabrication companies can be using different tooling & production processes, the materials can get stretched or deformed in different ways, or result in slightly different internal/external radiuses in non-important places, so we leave our sheet metal settings (bend radius, kFactor, unfold rules) at (mostly) default settings, rarely include detailed flat pattern views in their drawings, and if we do, we point out that it is just for reference, and that the folded part specs are of primary concern.  In other words, we don't care how they get there, as long as the result meets the finished/folded part specs, and 'our" flat pattern may not be exactly what they would need to 'cut' before bending to get there.

Because of this, I generally don't need any code based solutions for creating sheet metal flat patterns, or views of them, or DXF's of them...I just help others with their code for achieving those things because I understand most of what is involved, and like to help others when I have the time and resources to do so, while getting my other work done.  I usually learn quite a bit along the way too, which is a reward of its own, as well as getting faster at throwing similar codes together later, if needed.  Utilizing the custom iLogic Snippets for saving useful chunks of custom code also helps when assembling new custom codes faster, when needed.  Unfortunately, I haven't done the best job of also keeping URL Links to where I first posted or copied the original code chunk to/from, so that I can refer others back to it later, instead of just posting the code again somewhere else.  🤔

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 16 of 17

GosponZ
Collaborator
Collaborator

I do lot flat pattern since we have laser and banding machine. In that rule whoever is using can use also in assemblies. I like people who like to share knowledge and help other people. Main reason of forum is to help who need help. 

But as always i appreciate your help.

Message 17 of 17

dusan.naus.trz
Advisor
Advisor

I automated the code a little more. My problem is that I can't start writing code, I can read and edit, but starting is the hardest.

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.", vbOKOnly + vbCritical, "Assembly functions (iam)")
		'MsgBox("Chybně spuštěn dialog! Špatný typ dokumentu." & vbLf & "OK pro Ukončení." & vbLf & "(" & iLogicVb.RuleName & ") ", vbOKOnly + vbCritical, "Funkce Sestavy (iam)")
		Exit Sub
	End If
	
	Dim oDoc As Inventor.Document = ThisDoc.Document
	UnFoldSheetMetalPart(oDoc, True, True, True)
	For Each oRefDoc In oDoc.AllReferencedDocuments
		UnFoldSheetMetalPart(oRefDoc, True, True, True)
	Next
	oDoc.Update2(True)
End Sub

Sub UnFoldSheetMetalPart(doc As Inventor.Document, _
	Optional UseASideFaceIfAvailable As Boolean = False, _
	Optional PromptForFace As Boolean = False, _
	Optional CreateASideDefinition As Boolean = False)

	If (doc Is Nothing) OrElse (Not TypeOf doc Is PartDocument) Then Return
	Dim oPDoc As PartDocument = doc
	If Not TypeOf oPDoc.ComponentDefinition Is SheetMetalComponentDefinition Then Return
	Dim oSMDef As SheetMetalComponentDefinition = oPDoc.ComponentDefinition
	If oSMDef.HasFlatPattern Then Return
	If Not oPDoc.IsModifiable Then
		'        Logger.Warn("Soubor plechového dílu bez rozvinutí není modifikovatelný:" _
		'        & vbCrLf & oPDoc.DisplayName)
		MessageBox.Show("Following sheet metal part with no FlatPattern was 'Not Modifiable':" _
		& vbCrLf & oPDoc.DisplayName, "Title")
		Return
	End If

	Dim oBaseFace As Face = Nothing
	Dim oASideFace As Face = Nothing
	Dim oNewView As Inventor.View = Nothing
	Dim oASideDefs As ASideDefinitions = oSMDef.ASideDefinitions

	If UseASideFaceIfAvailable = True Then
		If (Not oASideDefs Is Nothing) AndAlso oASideDefs.Count > 0 Then
			oASideFace = oASideDefs.Item(1).ASideFace
			oBaseFace = oASideFace
		End If
	End If

	If PromptForFace = True Then
		If oBaseFace Is Nothing Then
			' Make the part visible and set it as the active document.
			If oPDoc.Views.Count = 0 Then
				oNewView = oPDoc.Views.Add()
				oNewView.Activate()
			Else
				oPDoc.Views.Item(1).Activate()
			End If

			' Automatic selection of the largest area (A-side) based on content
			Dim oCompDef As PartComponentDefinition = oPDoc.ComponentDefinition
			Dim oMaxFace As Face = Nothing
			Dim maxArea As Double = 0

			For Each oBody As SurfaceBody In oCompDef.SurfaceBodies
				For Each oFace As Face In oBody.Faces
					Dim area As Double = oFace.Evaluator.Area
					If area > maxArea Then
						maxArea = area
						oMaxFace = oFace
					End If
				Next
			Next
			oBaseFace = oMaxFace

			If oBaseFace Is Nothing Then Return
		End If
		If CreateASideDefinition = True Then
			oASideFace = oBaseFace
			' Add an A-side definition for this surface
			oASideDefs.Add(oASideFace)
		End If
	End If

	If oBaseFace Is Nothing Then
		Try
			oSMDef.Unfold()
		Catch ex As Exception
			'            Logger.Error("Chyba při volání SheetMetalComponentDefinition.Unfold pro:" & _
			'            vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
			MessageBox.Show("SheetMetalComponentDefinition.Unfold method error for:" & _
			vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
		End Try
	Else
		Try
			oSMDef.Unfold2(oBaseFace)
		Catch ex As Exception
			'            Logger.Error("Chyba při volání SheetMetalComponentDefinition.Unfold2 pro:" & _
			'            vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
			MessageBox.Show("SheetMetalComponentDefinition.Unfold2 method error for:" & _
			vbCrLf & oPDoc.DisplayName & vbCrLf & ex.ToString())
		End Try
	End If

	If oNewView IsNot Nothing Then oNewView.Close()
End Sub

 

0 Likes