iLogic rule for 'Before Save Document' under Event Triggers

iLogic rule for 'Before Save Document' under Event Triggers

checkcheck_master
Advocate Advocate
1,063 Views
9 Replies
Message 1 of 10

iLogic rule for 'Before Save Document' under Event Triggers

checkcheck_master
Advocate
Advocate

I thought it would be useful to write an iLogic rule for under 'Before Save Document' that checks for itself which file type we are dealing with. I'm finding out that this works differently then I thought. For example, when a part is re-stored from an assembly, the iLogic rule assumes we are dealing with an assembly and as such executes the assembly rules on the part. I had hoped that the rule would apply to the file that is currently being saved. Am I doing something wrong? Can I set the rule in such a way that it takes the component that is stored/saved at that moment?
I now use:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
 

 

 

0 Likes
Accepted solutions (1)
1,064 Views
9 Replies
Replies (9)
Message 2 of 10

Curtis_Waguespack
Consultant
Consultant

Hi @checkcheck_master 

 

I didn't understand what you meant by "restored" but, you might try this:

 

rather than this:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument

 

use this:

Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument

 

or if the rule is in the part, or being triggered on the part, try to use this:

Dim oDoc As Inventor.Document = ThisDoc.Document

 

It depends on what you have going on in the code, but if for instance you have an assembly and are opening each part in it with code (such as Thisapplication.Documents.open(c:\temp\mypart.ipt) then using ActiveEditDocument will grab that part document.

 

ThisDoc.Document will return the document that the rule resides in (or for external rules, the rule the document that the rule is triggered on).

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

EESignature

Message 3 of 10

checkcheck_master
Advocate
Advocate

Thank Curtis for your reaction, I'm gonna try some things.

 

With restored I meant the window which appears when you save an assembly and tells you which parts/sub-assemblies needs to be updated-saved.

 

0 Likes
Message 4 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @checkcheck_master.  I believe I understand the situation now.  You have a part document that contains a local iLogic rule, and you want that rule to be triggered to run each time that part document gets saved, right?  I bet you only intend for that rule to effect that 'local' part document, right?  Then when this part is being referenced within an assembly, and you save that assembly, it also wants to save this part, which is then triggering this rule to run, right?  Well, in that situation, the assembly is the 'active' document, so if your local rule is defining its 'target document' using ThisApplication.ActiveDocument, then it will be pointing to that assembly, instead of the 'local' part.  If you always want a local rule to be targeting its 'local' document, you should always use the 'ThisDoc.Document' phrase to define it.  In a local rule, that phrase will always point to the local document first.  If there is no local document, this phrase will then default to whichever document was 'active' when the rule first started.

 

Other things to watch out for in your local rules in these scenarios are some of the built-in iLogic snippets for working with iProperties & parameters.  These snippets often target whichever document is currently 'active' by default, unless you specifically include a document reference for it to target.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 10

checkcheck_master
Advocate
Advocate

Thanks a lot!

 

I was using:

Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument

 

And this does the trick:

Dim oDoc As Inventor.Document = ThisDoc.Document

 

 

 

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor

Any time @checkcheck_master.  Always glad to help when I can.  But don't forget to thank @Curtis_Waguespack too, because he actually suggested this solution first. 😏

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 10

checkcheck_master
Advocate
Advocate

You are absolutely right, thanks a lot too Curtis!

 

I'll take the opportunity to ask a question right away, it's like you know what it's about.
In the same macro/rule, which is btw an external rule that should work for all types of documents, which it does now and I'm very happy with.
Anyway, the rule checks for a certain iProperty, if it isn't there the user is shown an InputListBox where he can choose a value, at the same time the part is opened and activated so that the user can see which part it is about.
I would like to deactivate the part afterwards if it was not open before.

I don't know how to compare whether the part is already open, it is loaded I know in the background but...

And I don't know how to deactivate the part to put it back in the background.

Is this possible?

 

See the specific piece of code and the complete code.

 

Is the way in which the iProperties are allocated sufficiently stable or can it be improved?
In other words, if you see things that could be better, I'd be very happy to recommend it, thanks in advance.

See the specific piece of code and the complete code.

Try
				If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
					
					' Open document so that the user can select the Fabrication value, close if it was not opened
					ThisApplication.Documents.Open(oDoc.FullFileName, True) ' Visible
					ThisDoc.Document.Activate
					'MessageBox.Show(bool)
										
					CreateFabrication
					
					'ThisDoc.Document.Activate
				End If
			Catch
					CreateFabrication
			End Try

 

 
 Sub Main

''----------------------------------------------------------------------------------------------------
'' Write to textfile
'Dim strDateAndTime As String
'strDateAndTime = DateTime.Now.ToString(("yyyy-MM-dd HHmmss"))
Dim strTime As String
strTime = DateTime.Now.ToString(("HH:mm:ss"))
'Dim LogFileName As String
'LogFileName = "C:\temp\iLogic_Log_InventorPLUS " & strDateAndTime & ".txt"
'oWrite = System.IO.File.CreateText(LogFileName)

''----------------------------------------------------------------------------------------------------
'' Calculate elapsed time
'Dim oWatch As New Stopwatch
'oWatch.Start

'----------------------------------------------------------------------------------------------------
'Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument ' In case of an assembly from where components needs to be updated oDoc stays at the assy. 
Dim oDoc As Inventor.Document = ThisDoc.Document ' With ThisDoc.Document oDoc is set to the actual component is 
Logger.Info(strTime & " InventorPLUS: Start")
Logger.Info(strTime & " InventorPLUS: " & oDoc.FullDocumentName & vbNewLine)

' Try to run external rule, does not work, it seems that Inventor can't wait for the macro to do its thing...
'iLogicVb.RunExternalRule("C:\Workingfolder\CAD Settings\Inventor\Macros\Collapse_Browser.iLogicVb")
'InventorVb.RunMacro("ApplicationProject", "Collapse_Browser", "Collapse_Browser")
'iLogicVb.UpdateWhenDone = True
'Threading.Thread.Sleep(1000) 'in ms

' -------------------------------------------------------------------------------------------------------------------------------------------------------------	
		' Try to remove 'old' iLogic Rules and Event Triggers
'		Try
'			auto = iLogicVb.Automation
'			Dim iLogicAuto As Object
'			iLogicAuto = auto
'			Try: iLogicAuto.DeleteRule(oDoc, "PartParameters"): Catch: End Try
'			Try: iLogicAuto.DeleteRule(oDoc, "SheetMetalParametersIniProperties") :Catch: End Try
			
'		Catch
'		End Try
		
'		Dim customPropSets As PropertySets
'		customPropSets = oDoc.PropertySets
'		Dim customPropSet As PropertySet
'		customPropSet = oDoc.PropertySets.Item("_iLogicEventsRules")
		
'		Dim oProperty As Inventor.Property
'		oProperty = cuscustomPropSetq2.Delete("PartParameters")
'		oProperty = customPropSet.Delete("SheetMetalParametersIniProperties")

' -------------------------------------------------------------------------------------------------------------------------------------------------------------
'If ThisApplication.ActiveDocumentType = DocumentTypeEnum.kPartDocumentObject Then
If oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then	
	Logger.Info(strTime & " InventorPLUS: Inside PartDocument"& vbNewLine)	
	' Set Home
	Call ThisApplication.ActiveView.GoHome
	'MessageBox.Show("Wait...")
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Update iProperties
	Dim oCustomPropSet As PropertySet
	' Define custom property collection
	oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")

	Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Afmetingen").Delete : Catch : End Try
	Try : oCustomPropSet.Item("FPL").Delete : Catch : End Try
	Try : oCustomPropSet.Item("FPW").Delete : Catch : End Try
	Try : oCustomPropSet.Item("ERP Number").Delete : Catch : End Try
	Try : oCustomPropSet.Item("INK").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Type").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Remark").Delete : Catch : End Try	
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Check for Normal Part or Sheet Metal Part
	If oDoc.DocumentSubType.DocumentSubTypeID = "{4D29B490-49B2-11D0-93C3-7E0706000000}" Then ' Normal Part(No Sheetmetal) Normal Part(No Sheetmetal) Normal Part(No Sheetmetal)
	Logger.Info(strTime & " InventorPLUS: Normal Part(No Sheetmetal) " & vbNewLine)
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' Set iProperty 'PartParameters'
		Try
			PartLength = Round(Measure.ExtentsLength, 1)
			PartWidth = Round(Measure.ExtentsWidth, 1)
			PartHeight = Round(Measure.ExtentsHeight, 1)

			'PartLength = Ceil(Round(Measure.ExtentsLength, 1)/1)*1
			'PartWidth = Ceil(Round(Measure.ExtentsWidth, 1)/1)*1
			'PartHeight = Ceil(Round(Measure.ExtentsHeight, 1)/1)*1

			iProperties.Value("Custom", "PartParameters") = PartLength & " x " & PartWidth & " x " & PartHeight
			'iProperties.Value("Custom", "Mass") = Round(iProperties.Mass * 1, 1) & " kg"
			
		Catch
		End Try
	
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' Delete and update Sheetmetal related iProperties
		Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
		Try : oCustomPropSet.Item("CSINK").Delete : Catch : End Try
		Try : oCustomPropSet.Item("CBORE").Delete : Catch : End Try	
		Try : oCustomPropSet.Item("Tapping").Delete : Catch : End Try
		'Try : oCustomPropSet.Item("Thickness").Delete : Catch : End Try
		
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' In a Sheetmetal part Thickness as Sheetmetal property is exported to an iPropery
		' Undo this when a part is no longer a Sheetmetal part
		' Attention: Normaly this iProperty can be accesed by 'oParam = oCompDef.Thickness' 
		Try
			Dim oCompDef As PartComponentDefinition
			oCompDef = oDoc.ComponentDefinition

			' Get a reference to the parameter controlling the thickness.
			Dim oParam As Parameter
			'oParam = oCompDef.Thickness
			'oParam.ExposedAsProperty = False
			oParam = oCompDef.Parameters.Item(1)
			If oCompDef.Parameters.Item(1).Name = "Thickness" Then 	oParam.ExposedAsProperty = False
		Catch
		End Try
				
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' Delete Fabrication when the value is Sheetmetal related
		Try
			If iProperties.Value("Custom", "Fabrication") = "B" Or iProperties.Value("Custom", "Fabrication") = "F" Then
				Try : oCustomPropSet.Item("Fabrication").Delete : Catch
					End Try				
			End If
		Catch
			
		End Try
		
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------	
		' Check if iPropery Fabrication exist, if not create
		' In iLogic it's not possible to check if a iProperty exist, hence it's done in a function
		' Creating the iPropery is done in a Sub		
		
		'If oDoc.FileSaveCounter = 0 Then
			Try
				If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
					
					' Open document so that the user can select the Fabrication value, close if it was not opened
					ThisApplication.Documents.Open(oDoc.FullFileName, True) ' Visible
					ThisDoc.Document.Activate
					'MessageBox.Show(bool)
										
					CreateFabrication
					
					'ThisDoc.Document.Activate
				End If
			Catch
					CreateFabrication
			End Try
		'End If
				
	End If
	
	' -------------------------------------------------------------------------------------------------
	' Check for Normal Part or Sheet Metal Part
	If oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then ' Sheetmetal Part Sheetmetal Part Sheetmetal Part Sheetmetal Part Sheetmetal Part
	Logger.Info(strTime & " InventorPLUS: Sheetmetal Part" & vbNewLine)
		
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' In a Sheetmetal part Thickness as Sheetmetal property is exported to an iPropery to get Thickness on a drawing
				Dim oCompDef As SheetMetalComponentDefinition
		oCompDef = oDoc.ComponentDefinition

		' Get a reference to the parameter controlling the thickness.
		Dim oParam As Parameter
		oParam = oCompDef.Thickness
		oParam.ExposedAsProperty = True
		oParam.CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kThreeDecimalPlacesPrecision 
		oParam.CustomPropertyFormat.ShowTrailingZeros = False 
		oParam.CustomPropertyFormat.ShowUnitsString = False 
		oParam.CustomPropertyFormat.Units = "mm" 
		
		' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' Set iProperty 'PartParameters'
		Try
			SheetMetalLength = Round(SheetMetal.FlatExtentsLength, 1)
			SheetMetalWidth = Round(SheetMetal.FlatExtentsWidth,1)

			If SheetMetalLength > SheetMetalWidth Then				
				iProperties.Value("Custom", "PartParameters") = SheetMetalLength & " x " & SheetMetalWidth
			End If

			If SheetMetalWidth > SheetMetalLength Then				
				iProperties.Value("Custom", "PartParameters") = SheetMetalWidth & " x " & SheetMetalLength
			End If

			If SheetMetalWidth = SheetMetalLength Then
				iProperties.Value("Custom", "PartParameters") = SheetMetalWidth & " x " & SheetMetalLength				
			End If
		Catch
		End Try
		
		' -------------------------------------------------------------------------------------------------
		' Set iProperty 'Bend'
		Dim bFlat As Boolean
				
		Dim oSMDef As SheetMetalComponentDefinition
		oSMDef = oDoc.ComponentDefinition
		
			'Check for Flat Pattern
			If oSMDef.FlatPattern Is Nothing Then					
				'Create Flat Pattern
				Try
					oSMDef.Unfold()
					oSMDef.FlatPattern.ExitEdit()
					oDoc.Close()
					bFlat = True
				Catch ' Failed to unfold part
					'MessageBox.Show("Failed to create Flat Pattern")
					bFlat = False
					Return
				End Try
			Else										
				bFlat = True			
			End If
		
		Try
			
			If bFlat = True
				Dim bends As FlatBendResults
				bends = oSMDef.FlatPattern.FlatBendResults
				
				'Count bends
				Dim count As Integer = 0					
				For Each Bend In oSMDef.Bends
					count += 1
				Next
				
				'Dim oCustomPropSet As PropertySet
				'oCustomPropSet = oDoc.PropertySets("Inventor User Defined Properties")			

				' Needs to be checked for mirrored parts! 'Check for/create property

				'Try
				'	intTest = oCustomPropSet.Item("Bends").Value
					 				
				'Catch ex As Exception
				'	oCustomPropSet.Add(count, "Bends")				 
				'End Try

				'messagebox.Show("Wait...")					
				
				Try
					If count = 1 Then 
						iProperties.Value("Custom", "Bends") = count & " Bend"
						
						Try
							If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "B+S" 
					    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "B+W"
	 				    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "B+Z"
						Else:  iProperties.Value("Custom", "Fabrication") = "B"
							End If			    					
						Catch     
							iProperties.Value("Custom", "Fabrication") = "B"
			            End Try
						
					ElseIf count > 1 Then
						iProperties.Value("Custom", "Bends") = count & " Bends"
						
						Try
							If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "B+S" 
					    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "B+W"
	 				    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "B+Z"
						Else:  iProperties.Value("Custom", "Fabrication") = "B"
							End If			 
			    		Catch     
							iProperties.Value("Custom", "Fabrication") = "B"
			            End Try
						
					Else
						iProperties.Value("Custom", "Bends") = "No Bends"
					
						Try
							If iProperties.Value("Custom", "Fabrication") Like "*+S*" Then: iProperties.Value("Custom", "Fabrication") = "F+S" 
					    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+W*" Then: iProperties.Value("Custom", "Fabrication") = "F+W"
	 				    ElseIf iProperties.Value("Custom", "Fabrication") Like "*+Z*" Then: iProperties.Value("Custom", "Fabrication") = "F+Z"
						Else:  iProperties.Value("Custom", "Fabrication") = "F"
							End If			 
			    		Catch     
							iProperties.Value("Custom", "Fabrication") = "F"
			            End Try		
					
					End If
				
				Catch
					iProperties.Value("Custom", "Fabrication") = "F"
				End Try
				
							
				'Logger.Info(iProperties.Value("Custom", "Bends"))
				Logger.Info(strTime & " InventorPLUS: Bends found: " & count & oDoc.FullDocumentName & vbNewLine)

			End If
		
		Catch
		End Try
	
		' -------------------------------------------------------------------------------------------------
		' Check for Hole types
		
		Dim features = oDoc.ComponentDefinition.Features
		Dim HoleType As String
		Dim Tapping As Boolean
		Dim CSINK As Boolean
		Dim CBORE As Boolean

		Dim numberOfThreadedHoleFeatures As Integer = 0
		For Each hole As HoleFeature In features.HoleFeatures
			
			'MessageBox.Show (hole.Suppressed)
			
			Select Case hole.HoleType
							
			Case 21505 
				HoleType = "kDrilledHole"     ' Hole is drilled (no countersink Or counterboring).	
				
			Case 21506
				HoleType = "kCounterSinkHole" ' Hole is countersunk.
				If hole.Suppressed = False Then CSINK = True
				
			Case 21507
				HoleType = "kCounterBoreHole" ' Hole is counterbored.
				If hole.Suppressed = False Then CBORE = True
			
		 	Case 21508
				HoleType = "kSpotFaceHole"    ' Hole is spotfaced.
			
			
			Case 83912192
				HoleType = "kHoleFeatureObject"
			
			End Select
						
			Logger.Info(strTime & " InventorPLUS: Hole type: " & HoleType & oDoc.FullDocumentName & vbNewLine)
			Logger.Info(strTime & " InventorPLUS: " & ObjectTypeEnum.kHoleFeatureObject & vbNewLine)
					    
			' Tapped Hole
			Dim infoTap As HoleTapInfo = hole.TapInfo
			If (infoTap IsNot Nothing) Then
			    If hole.Suppressed = False Then numberOfThreadedHoleFeatures = numberOfThreadedHoleFeatures + 1
			    ' Your hole changing code go's here
			End If				
		
		Next
		
		' Tapped Hole
		If numberOfThreadedHoleFeatures > 0 Then
			iProperties.Value("Custom", "Tapping") = "Yes"				
		Else
			iProperties.Value("Custom", "Tapping") = "No"				
		End If
		
		' Counter Sink Hole
		If CSINK = True Then
	        iProperties.Value("Custom", "CSINK") = "Yes"
		Else
			iProperties.Value("Custom", "CSINK") = "No"
	    End If
			
		' Counter Bore Hole
		If CBORE = True Then
	        iProperties.Value("Custom", "CBORE") = "Yes"
		Else
			iProperties.Value("Custom", "CBORE") = "No"
	    End If
				
		'MsgBox("Number of holes with thread: " & numberOfThreadedHoleFeatures)
		'Logger.Info("Number of holes with thread: " & numberOfThreadedHoleFeatures)
			
			' you can skip this loop if you just want to count the number of tread features
		'For Each thread As ThreadFeature In features.ThreadFeatures
		' Your thread changing code go's here
		'Next
		'MsgBox("Number of threadfeatures (on a extrude or cut feature): " _
		'	& features.ThreadFeatures.Count)		
	
	' -------------------------------------------------------------------------------------------------
	
	oDoc = Nothing
	
	End If
	
'ElseIf ThisApplication.ActiveDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
	Logger.Info(strTime & " InventorPLUS: Inside AssemblyDocument"& vbNewLine)
	
	' Set Home view does not work, it looks like Inventor can't wait for the Home position to be taken...
	'Call ThisApplication.ActiveView.GoHome
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------	
	' Import latest BOM Customization settings
    Dim oAsmDef As AssemblyComponentDefinition
    oAsmDef = oDoc.ComponentDefinition
    
    Dim oBOM1 As BOM
    oBOM1 = oAsmDef.BOM
    
    Dim filename As String    ' Assume the file exists
    filename = "C:\Workingfolder\CAD Settings\Inventor\Personal Settings\HML\BOM_Customization_Settings.xml"
    
    Call oBOM1.ImportBOMCustomization(filename)
    Beep
    
    ' Update Mass properties
    ThisApplication.CommandManager.ControlDefinitions.Item("AppUpdateMassPropertiesCmd").Execute
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------	
	' Check Assembly Type and set iProperty for Weldment
	
	If oDoc.DocumentSubType.DocumentSubTypeID = "{28EC8354-9024-440F-A8A2-0E0E55D635B0}" Then  ' Stands for Weldassy 
	Logger.Info("WeldSheetmetal Part")
	
		Try: iProperties.Value("Custom", "Fabrication") = "W": Catch: End Try
	
	Else		
		' Do not make empty! In case it's not an Inventor Weldment but still a Weld Assembly
		' Until we agree that all Weld Assemblies needs to be Inventor Weldments
		'Try: iProperties.Value("Custom", "Fabrication") = "": Catch: End Try 
	End If
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------	
			
		' Check if iPropery Fabrication exist, if not create, only ask for new Assemblies which are not saved yet...
		' In iLogic it's not possible to check if a iProperty exist, hence it's done in a function
		' Creating the iPropery is done in a Sub
		
		If oDoc.FileSaveCounter = 0 Then
			Try
				If iProperties.Value("Custom", "Fabrication") = "" Or CheckIfPropertyExist("Fabrication") = False Then
					CreateFabrication
				End If
			Catch
					CreateFabrication
			End Try
		End If
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Set iProperty PartParameters
	Try
		PartLength = Round(Measure.ExtentsLength, 1)
		PartWidth = Round(Measure.ExtentsWidth, 1)
		PartHeight = Round(Measure.ExtentsHeight, 1)

		'PartLength = Ceil(Round(Measure.ExtentsLength, 1)/1)*1
		'PartWidth = Ceil(Round(Measure.ExtentsWidth, 1)/1)*1
		'PartHeight = Ceil(Round(Measure.ExtentsHeight, 1)/1)*1

		iProperties.Value("Custom", "PartParameters") = PartLength & " x " & PartWidth & " x " & PartHeight
		'iProperties.Value("Custom", "Mass") = Round(iProperties.Mass * 1, 1) & " kg"
		
	Catch
	End Try
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Update iProperties
	Dim oCustomPropSet As PropertySet
	' Define custom property collection
	oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")

	'MessageBox.Show("Assy...")

	Try : oCustomPropSet.Item("Bends").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Threaded holes").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Afmetingen").Delete : Catch : End Try
	Try : oCustomPropSet.Item("INK").Delete : Catch : End Try
	Try : oCustomPropSet.Item("ERP Number").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Type").Delete : Catch : End Try
	Try : oCustomPropSet.Item("Remark").Delete : Catch : End Try
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	
'ElseIf ThisApplication.ActiveDocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
ElseIf oDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then	
	Logger.Info(strTime & " InventorPLUS: Inside DrawingDocument"& vbNewLine)	
	 
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Update Copied Model iProperties
	' Re-copy chosen iProperties from source model to the drawing
	Try 

	Dim oControlDef As ControlDefinition = ThisApplication.CommandManager.ControlDefinitions.​Item("UpdateCopiedModeliPropertiesCmd")
			
			oControlDef.Execute2(True)
	Catch
	End Try
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
		' Zoom all
	ThisApplication.CommandManager.ControlDefinitions.Item("AppZoomallCmd").Execute
	iLogicVb.UpdateWhenDone = True
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------
	' Update drawing to have up to date mass
	ThisApplication.ActiveDocument.update()
	
	' -------------------------------------------------------------------------------------------------------------------------------------------------------------

	
End If

Logger.Info(strTime & " InventorPLUS: End " & vbNewLine)
	
''----------------------------------------------------------------------------------------------------
''Calculate and report elapsed time.
'oWatch.Stop()
'oWrite.WriteLine(vbNewLine & "End of log" & vbNewLine & ("Elapsed Time: " & oWatch.ElapsedMilliseconds.ToString & " Milliseconds"))

''----------------------------------------------------------------------------------------------------
'' Close log file, optional open the log file
'oWrite.Close()
'ThisDoc.Launch(LogFileName)

'----------------------------------------------------------------------------------------------------

End Sub

Function CheckIfPropertyExist(strProp As String) As Boolean

	' Check if Fabrication exist
	Try
		Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument
		Dim oCustomPropSet As PropertySet
		' Define custom property collection
		oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")
		' Try to see if a value is there for Fabrication
		Prop = oCustomPropSet.Item(strProp).Value
		CheckIfPropertyExist = True
	Catch
		CheckIfPropertyExist = False
	End Try

End Function

Sub CreateFabrication
	
	Dim oDoc As Inventor.Document = ThisApplication.ActiveEditDocument
	Dim oCustomPropSet As PropertySet
	' Define custom property collection
	oCustomPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties")

	Try: oCustomPropSet.Add("", "Fabrication"): Catch: End Try ' Add propery with empty value: catch
	Dim oList As New ArrayList
	oList.Add("S = Machining")
	oList.Add("W = Weldment")
	oList.Add("Z = Sawing")
	'oList.Add(" Empty")
	
	AskForValue:
	InputValue = Left (InputListBox("Please select a value", oList, InputValue ,Title := "InventorPLUS", ListName := "Fabrication"),1)			
	'InputValue = InputBox("Please enter a value for Fabrication", "InventorPLUS", "")
	If InputValue = "" Then
		'GoTo AskForValue
	Else
		iProperties.Value("Custom", "Fabrication") = InputValue
	End If

End Sub
Message 8 of 10

WCrihfield
Mentor
Mentor

Whoa...! 😲 That's a long iLogic rule.  First of all, I would be pretty tempted to break that thing down into multiple rules, then run them sequentially, so it is easier to follow and edit.

As for your first question about 'deactivating the document' if it was not open before, after you are done with it.  Do you really mean 'deactivate' or, since the document may not have been open before, would simply closing that document work better?  If all you want to do is 'deactivate' this document, while leaving it open in the background, all you have to do is 'Activate' a different document.  If you want to close it, then the document object has a Close method.  There is also a third option, but it might not be right for this application, called called ReleaseReference.  This is usually only used when you have opened a document 'invisibly' (specified Visibility = False when opening it).  If you use this instead of Close, there is another tool called CloseAll, which you can set to True, and it will free up any references (and memory) used by those unreferenced documents that were dealt with that way.

 

There are several ways to check if a document is open.  Each document object has an Open property (Boolean) which indicates if it is open.  You can check within the ThisApplication.Documents collection, or more specifically if you are only looking for the ones that are 'visibly' open, you can look in ThisApplication.Documents .VisibleDocuments.

 

As far as a stable way to access iProperties...  Using the iProperties.Value() method will always target whichever document is currently 'active' at that moment.  It does allow you to specify a document name as part of the input variable, but I generally avoid that in favor of using the standard API Object Model structure to get to the iProperties.  That way I can be 100% sure.  Do do that, just follow this path Document.PropertySets.PropertySet.Property.Value.  If you aren't familiar with what each of the 4 standard iProperty sets are called or which iProperties are within each set, I have attached both an Excel spreadsheet & a PDF of that chart, which extensively details it all out.

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 10

checkcheck_master
Advocate
Advocate

Thanks again for your detailed answer, very educational, I appreciate it very much.

 

Yes it's a long rule, a lot of different things happen, although that is of course relative.
Parts, Assemblies and Drawings each have a separate piece of code, Parts are again divided into Sheetmetal or not.
So it looks worse than it is, but I agree.

 

My motive is to have one(1) rule under 'All documents - Before Save Document'.
In my opinion that is the most flexible and it provides templates without fuss.
What is your view on this, am I exaggerating?
Can you tell me what happens in terms of performance when I put 10 smaller rules under 'All Document - Before Save Document' for a Part Document?


Other thing, Event Triggers are imbedded in the Application Settings, if I'm right?
In order to spread the correct settings over different machines the settings have to be transferred from one machine to another, not everyone is happy with my settings just to have the correct Event Triggers set. Could that be an argument to have less but more extensive rules?

 

I went for your first option to hide the activated component, bring out something else. Very effective.

As for the iProperties it seems to be going well, I try to use ThisDoc where possible to point to the correct model.
As far as I can see it seems to be going well.

I have one more thing with:
'Call ThisApplication.ActiveView.GoHome', I can't really figure out how to do that from ThisDoc.
Is there anything for that?

0 Likes
Message 10 of 10

WCrihfield
Mentor
Mentor

There's nothing wrong with utilizing the All Documents tab of the Event Triggers dialog.  And the argument for smaller rules vs larger ones is mostly just a personal preference, and ease of use thing, so it's not really that important.  If it is working for you and your team, that's the most important thing.  Your preferences and opinions about such things may change over time as you get more experienced in these matters.  In general, if I have a really large or complex code, it is usually best to leave yourself (or someone else) plenty of comments about what's going on, which you seem to be doing a fairly good job of already.  But I wasn't talking about having all the other rules also under the All Documents tab though.  There are two ways I had in mind for splitting this up into multiple smaller rules.  If they are split up by the document type they are dealing with, then they could be put within the appropriate document type tab of the Event Triggers.  The other thought was to have a main smaller rule, that is the only one listed under the All Documents tab, then somewhere within that rule (usually near the end), have lines of code to run the other rules, as needed.

 

The Event Triggers you set up within the 'All Documents', 'Parts', 'Assemblies', 'Drawings' tabs are stored in an external XML file (I believe), so they aren't really within the Application Options.  Then the Event Triggers you set-up within the 'This Document' tab are saved within a 'hidden' PropertySet (iProperties) within the document.

 

As for being able to use the 'GoHome' tool from ThisDoc, here is something you may be able to use:

'This is not reffering to a DrawingView, just an Inventor.View
ThisDoc.Document.Views.Item(1).GoHome

There are also some other useful related tools under that View object (like Fit).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)