Need help with a code

Need help with a code

GKR2023
Enthusiast Enthusiast
301 Views
2 Replies
Message 1 of 3

Need help with a code

GKR2023
Enthusiast
Enthusiast

Hello,

 

I found this code here:

Solved: Define A-Side from Assembly - Autodesk Community - Inventor

 

But when I try the code it wont work it doesn't define the A-side.

I am trying to do the following:

In an assembly, it needs to define the A-Side from all the underlaying sheet metal parts F.E. the bottem view. 

If ThisApplication.ActiveDocumentType = "12290" Then

	Dim oDoc As PartDocument = ThisApplication.ActiveDocument
	Dim face As Inventor.Face
	Dim oCompDef As SheetMetalComponentDefinition
	oCompDef = oDoc.ComponentDefinition
	
	If oCompDef.ASideFaceStatus.ToString <> "kASideUpToDate" Then
		MessageBox.Show("MUST DEFINE A-SIDE " & iProperties.Value("Project", "Part Number"), "DEFINE A-SIDE", MessageBoxButtons.OK, MessageBoxIcon.Stop)

		iLogicVb.RunExternalRule("DELETE FLAT PATTERNS")

		face = ThisApplication.CommandManager.Pick(Inventor.SelectionFilterEnum.kPartFaceFilter, "Define A-Side")

		oDoc.SelectSet.Select(face)
		ThisApplication.CommandManager.ControlDefinitions.Item("SheetMetalCreateASideCmd").Execute2(True)
		oDoc.SelectSet.Clear
		oDoc.ComponentDefinition.Unfold
		oDoc.ComponentDefinition.FlatPattern.ExitEdit
	End If
End If

 

' Check the document type
If ThisApplication.ActiveDocument.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	' Define the active assembly
	Dim oAssyDoc As AssemblyDocument
	oAssyDoc = ThisApplication.ActiveDocument
	' Check all referenced docs 
	For Each oDoc As Document In oAssyDoc.AllReferencedDocuments
		' Check if the part is sheet metal
		If oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
			' Get sheet metal component definition
			Dim oSMCD As Inventor.SheetMetalComponentDefinition
			oSMCD = oDoc.ComponentDefinition
			' Check If it has flatpattern
			If oSMCD.HasFlatPattern = True Then
				' Atempt to delete it
				Try
					oSMCD.FlatPattern.Delete
				Catch
				End Try
			End If
		End If
	Next
ElseIf ThisApplication.ActiveDocument.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
	' Get the sheetmetal component definition
	Dim oSMCD As Inventor.SheetMetalComponentDefinition
	oSMCD = ThisApplication.ActiveDocument.ComponentDefinition
	' Check If it has flatpattern
	If oSMCD.HasFlatPattern = True Then
		' Atempt to delete it
		Try
			oSMCD.FlatPattern.Delete
		Catch
		End Try
	End If
End If
' Update the files
InventorVb.DocumentUpdate()

 

Ciao
0 Likes
Accepted solutions (1)
302 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @GKR2023.  I have an iLogic rule for you to check out.  I have not tested it yet myself though, because I have no use for it, so read through it first, and proceed with caution.  This does use the 'Pick' method, like the other examples, but it does not execute the command to show the ASideDefinition dialog, it just adds the 'picked' face when creating a new ASideDefinition, using the normal built-in Inventor API method.  There are a lot of detailed steps involved, and I tried to use multiple Try...Catch statements in there, where something might possibly fail, with information feedback written to the iLogic Log window if something doesn't work, to help with any potential debugging that may be needed later.  Let me know how this works for you.

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbCritical, "")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oOccs As ComponentOccurrences = oADoc.ComponentDefinition.Occurrences
	RecurseComponents(oOccs, AddressOf ProcessComponent)
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	MsgBox("This rule's work has finished.", vbInformation,"Job Is Done!")
End Sub

Sub RecurseComponents(oComps As ComponentOccurrences, ComponentProcess As Action(Of ComponentOccurrence))
	If oComps Is Nothing OrElse oComps.Count = 0 Then Exit Sub
	For Each oComp As ComponentOccurrence In oComps
		ComponentProcess(oComp)
		If oComp.Suppressed = False AndAlso _
			oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			RecurseComponents(oComp.Definition.Occurrences, ComponentProcess)
		End If
	Next
End Sub

Sub ProcessComponent(oComp As ComponentOccurrence)
	If oComp Is Nothing OrElse oComp.Suppressed Then Exit Sub
	If TypeOf oComp.Definition Is SheetMetalComponentDefinition = False Then Exit Sub
	Dim oSMDef As SheetMetalComponentDefinition = oComp.Definition
	Dim oFP As FlatPattern = Nothing
	If oSMDef.HasFlatPattern Then oFP = oSMDef.FlatPattern
	Dim oADefs As ASideDefinitions = oSMDef.ASideDefinitions
	Dim oADef As ASideDefinition = Nothing
	If oADefs.Count > 0 Then oADef = oADefs.Item(1)
	If oADef Is Nothing OrElse oADef.Status <> ASideFaceStatusEnum.kASideUpToDate Then
		Dim oSMDoc As PartDocument = oSMDef.Document
		Dim sFDN As String = oSMDoc.FullDocumentName
		If oFP IsNot Nothing Then
			Try
				oFP.Delete
			Catch
				Logger.Error("Error deleting FlatPattern of following document:" & vbCrLf & sFDN)
				Exit Sub
			End Try
		End If
		If oADef IsNot Nothing Then
			Try
				oADef.Delete
			Catch
				Logger.Error("Error deleting ASideDefinition of following document:" & vbCrLf & sFDN)
			End Try
		End If
		'now define the new ASideDefinition, then re-create the FlatPattern
		oComp.Edit 'enter into Edit Mode of this component (Important)
		Dim oNewASideFace As Face = Nothing
		oNewASideFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Select Flat Face For ASideDefinition.")
		If oNewASideFace Is Nothing Then
			oComp.ExitEdit(ExitTypeEnum.kExitToTop)
			Exit Sub
		End If
		Try
			oADef = oADefs.Add(oNewASideFace)
		Catch
			Logger.Error("Error defining new ASideDefinition in following document:" & vbCrLf & sFDN)
		End Try
		'now attempt to re-create the FlatPattern using that face as its BaseFace
		Try
			oSMDef.Unfold
			'oFP = oSMDef.Unfold2(oNewASideFace) '<<<< not sure if this is what you wanted >>>>
		Catch
			Logger.Error("Error unfolding / creating FlatPattern for following document:" & vbCrLf & sFDN)
		End Try
		If oSMDoc.RequiresUpdate Then oSMDoc.Update2(True)
		oComp.ExitEdit(ExitTypeEnum.kExitToTop)
	End If
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 3 of 3

GKR2023
Enthusiast
Enthusiast

Pretty neat code thanks a lot again!

Ciao
0 Likes