Cant get code to run in part if open visually boolean isnt True

Cant get code to run in part if open visually boolean isnt True

jzcrouse
Enthusiast Enthusiast
784 Views
6 Replies
Message 1 of 7

Cant get code to run in part if open visually boolean isnt True

jzcrouse
Enthusiast
Enthusiast

 

So this code to run a rule in certain parts in a assembly works perfectly when the open part document boolean is True.
Dim oDoc As PartDocument = ThisApplication.Documents.Open(oFileName, True)

 

Its kind of annoying on large assemblies to have to watch every part be opened and closed. 

 

Is there a reason why the code wont run my rule when the boolean is false or is inventor just not able to run the external rule if the part is not opened visually and I just have to deal with it?

 

Thanks

Private Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisApplication.ActiveDocument


    ' Call the function that traverses the assembly

    Call Iterate(asmDoc.ComponentDefinition.Occurrences,1)
    
    ' Update the view.
    ThisApplication.ActiveView.Update
End Sub



Sub Iterate(Occurrences As ComponentOccurrences, Level As Integer)
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oPart As ComponentOccurrence
'set rules to be ran
Dim sRuleName As String = "BASE_QTY_CHANGE_RULE 1"

'''Iterate through part occurrences in assembly
 
	For Each oPart In Occurrences

		Dim oFileName As String = oPart.Definition.Document.FullFileName
'Check to see if occurance is a Part
		If oPart.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then

'Set part type to be changed.
			If oPart.Name.Contains("STR.") Then
				'Open Part document True opens visually.
				Dim oDoc As PartDocument = ThisApplication.Documents.Open(oFileName, True)
					auto = iLogicVb.Automation
				Try
					'run desired rule
					auto.RunExternalRule(oDoc, sRuleName)
				Catch
					MessageBox.Show("Cannot find a rule with the name " & sRuleName & "." & vbLf & "Please try again.", "Open and run")
				End Try
		'''Close the document with SAVE (as False), without SAVE (As True)
					oDoc.Close(False)
		
			End If
	'recursively call sub to traverse through occurance if it is a subassembly		
	ElseIf oPart.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            Call Iterate(oPart.SubOccurrences, Level)
        
		

	
End If
	
	Next

		
End Sub  

 

0 Likes
Accepted solutions (3)
785 Views
6 Replies
Replies (6)
Message 2 of 7

johnster100
Collaborator
Collaborator
Accepted solution

asmDoc = ThisApplication.ActiveDocument

 

ActiveDocument will not work if visibility is set to false.

 

If it's in iLogic then use:

asmDoc = ThisDoc.Document

 

I think that will work,

cheers,

John

0 Likes
Message 3 of 7

DRoam
Mentor
Mentor
Accepted solution

Using ActiveDocument should be fine, assuming you're running the rule from an open Assembly.

 

I believe the problem might be with the fact you're trying to Open and Close documents that are already open and being used by your assembly. The fact that those Part documents are occurrences in your active assembly means that they're already loaded into memory (in other words, opened). So you don't need to Open them. And closing them might cause issues because your assembly is using them.

 

I would make the following changes to your code (key change in blue -- a few other recommended changes in red/green):

 

	'Iterate through occurrences in assembly
	For Each oOcc As ComponentOccurrence In Occurrences
		'Dim oFileName As String = oPart.Definition.Document.FullFileName
		
		'Check to see if occurance is a Part
		If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
			If oOcc.Name.Contains("STR.") Then
				'Get Part Document object.
				'Dim oDoc As PartDocument = ThisApplication.Documents.Open(oFileName, True)
				Dim oDoc As PartDocument = oOcc.Definition.Document
				
				Dim auto As IiLogicAutomation = iLogicVb.Automation
				
				Try
					'run desired rule
					auto.RunExternalRule(oDoc, sRuleName)
				Catch
					MessageBox.Show("Cannot find a rule with the name " & sRuleName & "." & vbLf & "Please try again.", "Open and run")
				End Try
				oDoc.Close(False)
			End If
		ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
			'Recursively call sub to traverse through occurance if it is a subassembly
			Call Iterate(oOcc.SubOccurrences, Level)
		End If
	Next

 

0 Likes
Message 4 of 7

jzcrouse
Enthusiast
Enthusiast

Thanks for the input. I tried the suggestions. I do not believe it ThisDoc is working perhaps because the rules I am running are an external rules and not stored in the assembly. 

0 Likes
Message 5 of 7

DRoam
Mentor
Mentor

ThisDoc uses the active document if manually executed from an External Rule. If it's triggered by a document's Event Trigger, ThisDoc is the triggering document. So it should work fine.

 

Can you please post the error your getting? Or if there's no error, a detailed description of what's going wrong?

0 Likes
Message 6 of 7

jzcrouse
Enthusiast
Enthusiast

Here is what I ran. No error message. I put a message box in after the rule should be run with the doc name and everything seems to be working correctly. Except the effects of my sRuleName are not taking effect in the specified parts as there were previously. It seems like the rule is running in the Assembly and not the part maybe? But its confusing since the oDoc displayname is the correct part itp

Private Sub Main()
    Dim asmDoc As AssemblyDocument
    asmDoc = ThisDoc.Document


    ' Call the function that traverses the assembly

    Call Iterate(asmDoc.ComponentDefinition.Occurrences,1)
    
    ' Update the view.
    ThisApplication.ActiveView.Update
End Sub



Sub Iterate(Occurrences As ComponentOccurrences, Level As Integer)
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisDoc.Document.ComponentDefinition
Dim oPart As ComponentOccurrence
'set rules to be ran
Dim sRuleName As String = "BASE_QTY_CHANGE_RULE 1"

'''Iterate through part occurrences in assembly
 
	For Each oPart In Occurrences

'		Dim oFileName As String = oPart.Definition.Document.FullFileName
'Check to see if occurance is a Part
		If oPart.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then

'Set part type to be changed.
			If oPart.Name.Contains("STR.") Then
				'Open Part document True opens visually.

				Dim oDoc As PartDocument = oPart.Definition.Document
					
					Dim auto As IilogicAutomation = iLogicVb.Automation
				Try
					'run desired rule
					auto.RunExternalRule(oDoc, sRuleName)
					MessageBox.Show(oDoc.DisplayName)
				Catch
					MessageBox.Show("Cannot find a rule with the name " & sRuleName & "." & vbLf & "Please try again.", "Open and run")
				End Try
		'''Close the document with SAVE (as False), without SAVE (As True)

		
			End If
	'recursively call sub to traverse through occurance if it is a subassembly		
	ElseIf oPart.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
            Call Iterate(oPart.SubOccurrences, Level)
        
		

	
End If
	
	Next

		
End Sub  

  

0 Likes
Message 7 of 7

jzcrouse
Enthusiast
Enthusiast
Accepted solution

ah figured it out. I was using 

 Document = ThisApplication.ActiveDocument

in the rule i was trying to run. Changed it to Thisdoc.document and worked. 

Thanks for your help! 

0 Likes