Waiting until part of rule is complete to finish rule

Waiting until part of rule is complete to finish rule

KWarrenCA
Advocate Advocate
564 Views
2 Replies
Message 1 of 3

Waiting until part of rule is complete to finish rule

KWarrenCA
Advocate
Advocate

I've got a rule that creates a view, hides certain parts, creates simplified part, and match iProperties of the assembly. It works well with smaller assemblies but when it runs on larger assemblies the simplified part creation takes a while and then it doesn't save the file and copy the iProperties over. Is there any way to keep it from moving forward at line 114 until the earlier part of the rule is completed?

 

Class iRule
	Public oView As DrawingView
	Public oSheet As Sheet
	Public oPartslist As PartsList
	Public oCompDef As ComponentDefinition
	Public oNewFullName As String

	Sub Main()
		Q = MessageBox.Show("You are about to create an Asset View and Simplified Part this may take a few minutes to complete. Click Cancel to stop the rule from running.", "Simplified Part", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)

		If Q = vbOK Then 'Action 
		ElseIf Q = vbCancel Then
			Exit Sub
		End If

			oSrcDWG = ThisApplication.ActiveDocument
			oCompDef = oSrcDWG.ComponentDefinition

			Try
				oCompDef.RepresentationsManager.DesignViewRepresentations.Item("ASSET VIEW").Activate
				SimplifiedPartCreation(oSrcDWG)
				CopyIproperties(oSrcDWG)
				
			Catch
				ViewCreation(oSrcDWG)
				PartTurnOFF(oSrcDWG)
				CopyIproperties(oSrcDWG)
				SimplifiedPartCreation(oSrcDWG)
				CopyIproperties(oSrcDWG)
				
			End Try

		'CopyIproperties(oSrcDWG)
	End Sub

	Sub ViewCreation(oSrcDWG)
		'Creates Representation
		Dim oviewrep As DesignViewRepresentation
		Try
			oCompDef.RepresentationsManager.DesignViewRepresentations.Item("ASSET VIEW").Activate
		Catch
			oviewrep = oCompDef.RepresentationsManager.DesignViewRepresentations.Add("ASSET VIEW")
			'Activate new View Rep
			oviewrep.activate
		End Try
		'Turn off ALL object visibility

		ThisApplication.ActiveDocument.ObjectVisibility.AllWorkfeatures = False
		ThisApplication.ActiveDocument.ObjectVisibility.ConstructionSurfaces = False
		ThisApplication.ActiveDocument.ObjectVisibility.Sketches = False
		ThisApplication.ActiveDocument.ObjectVisibility.Sketches3D = False
		ThisApplication.ActiveDocument.ObjectVisibility.SketchDimensions = False
		ThisApplication.ActiveDocument.ObjectVisibility.Annotations3D = False
		If oSrcDWG.DocumentType = kAssemblyDocumentObject Then
			ThisApplication.ActiveDocument.ObjectVisibility.WeldmentSymbols = False
			ThisApplication.ActiveDocument.ObjectVisibility.Welds = False
		End If

		iLogicVb.UpdateWhenDone = True

	End Sub
	Sub PartTurnOFF(oSrcDWG)
		'Hides Parts in assembly for view
		Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
		Dim occ As Inventor.ComponentOccurrence
		For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
			Dim refDoc As PartDocument = occ.Definition.Document
			'MessageBox.Show(occ.Name.ToString, "Title")
			'MessageBox.Show(iProperties.Value(occ.Name,"Project", "Part Number").ToString, "Title")

			''ADD MORE IF STATEMENTS ON OTHER PARTS TO REMOVE
			If iProperties.Value(occ.Name, "Project", "Vendor").Contains("HARDWARE") Then
				occ.Visible = False
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("CLAMP") Then
			occ.Visible = False
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("GASKET") Then
			occ.Visible = False
			'USE TO TURN PARTS ON
			'occ.Visible = True
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("PLUG") Then
			occ.Visible = False
			End If
		Next
	End Sub

	Sub SimplifiedPartCreation(oSrcDWG)
		'Creates Simplified part 
		Try
			Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oSrcDWG.FullFileName)
			Dim oPartDoc As PartDocument
			Dim g_App As Inventor.InventorServer = ThisApplication

			Dim AssDoc As Inventor.AssemblyDocument = ThisDoc.Document

			'Create a new part document that will be the shrinkwrap substitute
			oPartDoc = g_App.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)

			Dim oPartDef As PartComponentDefinition
			oPartDef = oPartDoc.ComponentDefinition

			Dim oDerivedAssemblyDef As DerivedAssemblyDefinition
			oDerivedAssemblyDef = oPartDef.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AssDoc.FullDocumentName)
			' Set various shrinkwrap related options
			oDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
			oDerivedAssemblyDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
			Call oDerivedAssemblyDef.SetHolePatchingOptions(DerivedHolePatchEnum.kDerivedPatchNone)
			Call oDerivedAssemblyDef.SetRemoveByVisibilityOptions(DerivedGeometryRemovalEnum.kDerivedRemoveNone)
			Dim oDerivedAss As DerivedAssemblyComponent
			oDerivedAss = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)
			' Call oDerivedAss.SuppressLinkToFile(True)

			' Save the part
			Dim partname As String = ThisDoc.PathAndFileName(False) & " - SIMPLIFIED.ipt"
			ThisApplication.ActiveView.Fit
			ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
			Call oPartDoc.SaveAs(partname, False)

		Catch ex As Exception
			ErrorMessage = "Error creating ipt file."
		End Try
	End Sub

	Sub CopyIproperties(ByVal oSrcDWG As Document)
		'Used to Match iProperties to newly created part.
		toDoc = ThisApplication.ActiveDocument
		Dim fromPs As PropertySet
		For Each fromPs In oSrcDWG.PropertySets
			Dim toPs As PropertySet
			If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then
				toPs = toDoc.PropertySets.Add(fromPs.Name, fromPs.InternalName)
			Else
				toPs = toDoc.PropertySets(fromPs.InternalName)
			End If

			Dim fromP
			For Each fromP In fromPs
				Dim toP
				On Error Resume Next
				toP = toPs.ItemByPropId(fromP.PropId)
				If Err.Number <> 0 Then
					toP = toPs.Add(fromP.Value, fromP.Name, fromP.PropId)
				Else
					toP = toPs(fromP.Name)
					If fromP.Value <> Nothing Then
						toP.Value = fromP.Value
					End If
				End If
				On Error GoTo 0
			Next
		Next
		ThisApplication.ActiveView.Fit
		ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
		i = MessageBox.Show("Your Simplified part has been completed."& vbCrLf &"Verify your model is correct and finish these steps"& vbCrLf &" - Suppress link with base component"& vbCrLf &" - Check simplified part into the Vault"& vbCrLf &" - Publish Asset", "Asset Creation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

	End Sub
End Class

 

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

A.Acheson
Mentor
Mentor

Hi @KWarrenCA 

Not sure exactly what is causing the not waiting scenario. The setting of isometric view is driven by control definition so that usually doesn't wait before moving to next line of code. 

 

You can use a loop like this if there is known updates happening but you will need to  heck if this would indeed be activated in the right location.

Do While Document.RequiresUpdate() = False
	Call  ThisApplication.UserInterfaceManager.DoEvents
Loop

 

The other thing I noticed is that your using different versions of the document object such as ThisApplication.ActiveDocument and ThisDoc.Document so it would be best to stick with one and or pass through the object to be used later. Maybe run a check also on the document fullfilename to ensure your getting the right document active when working through the sub routine. Even confirm this with Document.Activate.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 3

KWarrenCA
Advocate
Advocate
Accepted solution

Thanks @A.Acheson for the help I wasn't able to get the do loop to work correctly but I separated the save as its own sub which then help. Below is the fixed code that made it work.

 

Class iRule
	Public oView As DrawingView
	Public oSheet As Sheet
	Public oPartslist As PartsList
	Public oCompDef As ComponentDefinition
	Public oNewFullName As String

	Sub Main()
		Q = MessageBox.Show("You are about to create an Asset View and Simplified Part this may take a few minutes to complete. Click Cancel to stop the rule from running.", "Simplified Part", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)

		If Q = vbOK Then 'Action 
		ElseIf Q = vbCancel Then
			Exit Sub
		End If

		oSrcDWG = ThisApplication.ActiveDocument
		oCompDef = oSrcDWG.ComponentDefinition

		Try
			oCompDef.RepresentationsManager.DesignViewRepresentations.Item("ASSET VIEW").Activate
			SimplifiedPartCreation(oSrcDWG)

			SaveFile(oSrcDWG)
			CopyIproperties(oSrcDWG)
		Catch
			ViewCreation(oSrcDWG)
			PartTurnOFF(oSrcDWG)
			SimplifiedPartCreation(oSrcDWG)

			SaveFile(oSrcDWG)
			CopyIproperties(oSrcDWG)
		End Try

		'CopyIproperties(oSrcDWG)
	End Sub

	Sub ViewCreation(oSrcDWG)
		'Creates Representation
		Dim oviewrep As DesignViewRepresentation
		Try
			oCompDef.RepresentationsManager.DesignViewRepresentations.Item("ASSET VIEW").Activate
		Catch
			oviewrep = oCompDef.RepresentationsManager.DesignViewRepresentations.Add("ASSET VIEW")
			'Activate new View Rep
			oviewrep.activate
		End Try
		'Turn off ALL object visibility

		ThisApplication.ActiveDocument.ObjectVisibility.AllWorkfeatures = False
		ThisApplication.ActiveDocument.ObjectVisibility.ConstructionSurfaces = False
		ThisApplication.ActiveDocument.ObjectVisibility.Sketches = False
		ThisApplication.ActiveDocument.ObjectVisibility.Sketches3D = False
		ThisApplication.ActiveDocument.ObjectVisibility.SketchDimensions = False
		ThisApplication.ActiveDocument.ObjectVisibility.Annotations3D = False
		If oSrcDWG.DocumentType = kAssemblyDocumentObject Then
			ThisApplication.ActiveDocument.ObjectVisibility.WeldmentSymbols = False
			ThisApplication.ActiveDocument.ObjectVisibility.Welds = False
		End If

		iLogicVb.UpdateWhenDone = True

	End Sub
	Sub PartTurnOFF(oSrcDWG)
		'Hides Parts in assembly for view
		Dim assemblyDef As AssemblyComponentDefinition = ThisDoc.Document.ComponentDefinition
		Dim occ As Inventor.ComponentOccurrence
		For Each occ In assemblyDef.Occurrences.AllLeafOccurrences
			Dim refDoc As PartDocument = occ.Definition.Document
			'MessageBox.Show(occ.Name.ToString, "Title")
			'MessageBox.Show(iProperties.Value(occ.Name,"Project", "Part Number").ToString, "Title")

			''ADD MORE IF STATEMENTS ON OTHER PARTS TO REMOVE
			If iProperties.Value(occ.Name, "Project", "Vendor")="HARDWARE" Then
				occ.Visible = False
				
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("CLAMP") Then
			occ.Visible = False
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("GASKET") Then
			occ.Visible = False
			'USE TO TURN PARTS ON
			'occ.Visible = True
			Else If iProperties.Value(occ.Name, "Project", "DESCRIPTION").Contains("PLUG") Then
			occ.Visible = False
			End If
		Next
	End Sub

	Sub SimplifiedPartCreation(oSrcDWG)
		'Creates Simplified part 
		Try
			Dim oFileName As String = IO.Path.GetFileNameWithoutExtension(oSrcDWG.FullFileName)
			Dim oPartDoc As PartDocument
			Dim g_App As Inventor.InventorServer = ThisApplication

			Dim AssDoc As Inventor.AssemblyDocument = ThisDoc.Document

			'Create a new part document that will be the shrinkwrap substitute
			oPartDoc = g_App.Documents.Add(DocumentTypeEnum.kPartDocumentObject, , True)

			Dim oPartDef As PartComponentDefinition
			oPartDef = oPartDoc.ComponentDefinition

			Dim oDerivedAssemblyDef As DerivedAssemblyDefinition
			oDerivedAssemblyDef = oPartDef.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(AssDoc.FullDocumentName)
			' Set various shrinkwrap related options
			oDerivedAssemblyDef.DeriveStyle = DerivedComponentStyleEnum.kDeriveAsMultipleBodies
			oDerivedAssemblyDef.IncludeAllTopLevelWorkFeatures = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLevelSketches = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLeveliMateDefinitions = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IncludeAllTopLevelParameters = DerivedComponentOptionEnum.kDerivedExcludeAll
			oDerivedAssemblyDef.IsAssociativeDesignView = True
			
			'oDerivedAssemblyDef.IncludeAllSketches
			'oDerivedAssemblyDef.IncludeAllParameters
			
			Call oDerivedAssemblyDef.SetHolePatchingOptions(DerivedHolePatchEnum.kDerivedPatchNone)
			Call oDerivedAssemblyDef.SetRemoveByVisibilityOptions(DerivedGeometryRemovalEnum.kDerivedRemoveNone)
			Dim oDerivedAss As DerivedAssemblyComponent
			oDerivedAss = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAssemblyDef)
		
'			'Call oDerivedAss.SuppressLinkToFile(True)

		Catch ex As Exception
			ErrorMessage = "Error creating ipt file."
		End Try
	End Sub
	Sub SaveFile(oSrcDWG)
		toDoc = ThisApplication.ActiveDocument
		Dim partname As String = ThisDoc.PathAndFileName(False) & " - SIMPLIFIED.ipt"
		ThisApplication.ActiveView.Fit
		ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
		Call toDoc.SaveAs(partname, False)
	End Sub

	Sub CopyIproperties(ByVal oSrcDWG As Document)
		'Used to Match iProperties to newly created part.
		toDoc = ThisApplication.ActiveDocument
		Dim fromPs As PropertySet
		For Each fromPs In oSrcDWG.PropertySets
			Dim toPs As PropertySet
			If Not toDoc.PropertySets.PropertySetExists(fromPs.InternalName) Then
				toPs = toDoc.PropertySets.Add(fromPs.Name, fromPs.InternalName)
			Else
				toPs = toDoc.PropertySets(fromPs.InternalName)
			End If

			Dim fromP
			For Each fromP In fromPs
				Dim toP
				On Error Resume Next
				toP = toPs.ItemByPropId(fromP.PropId)
				If Err.Number <> 0 Then
					toP = toPs.Add(fromP.Value, fromP.Name, fromP.PropId)
				Else
					toP = toPs(fromP.Name)
					If fromP.Value <> Nothing Then
						toP.Value = fromP.Value
					End If
				End If
				On Error GoTo 0
			Next
		Next

		'i = MessageBox.Show("All iProperties have been copied", "iProperties have been updated", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
ThisApplication.ActiveDocument.UnitsOfMeasure.lengthunits = UnitsTypeEnum.kInchLengthUnits
			ThisApplication.ActiveDocument.UnitsOfMeasure.Massunits = UnitsTypeEnum.kLbMassMassUnits
			ThisApplication.ActiveDocument.ComponentDefinition.Material.Name = "AS SUPPLIED"
		ThisApplication.ActiveView.Fit
		ThisApplication.CommandManager.ControlDefinitions.Item("AppIsometricViewCmd").Execute
		i = MessageBox.Show("Your Simplified part has been completed." & vbCrLf & "Verify your model is correct and finish these steps" & vbCrLf & " - Suppress link with base component" & vbCrLf & " - Check simplified part into the Vault" & vbCrLf & " - Publish Asset", "Asset Creation", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

	End Sub


End Class