Run External Rules on all drawings from assembly

Run External Rules on all drawings from assembly

bsnyderACLUW
Enthusiast Enthusiast
1,090 Views
3 Replies
Message 1 of 4

Run External Rules on all drawings from assembly

bsnyderACLUW
Enthusiast
Enthusiast

Hi everyone,

 

I am currently trying to put together a rule that will run a series of rules from an assembly. The goal is to have the rule open each drawing, one by one, associated to each part in the assembly and run a set of external rules before closing the drawing an moving onto the next. Any help with this would be greatly appreciated.

 

Below is the code I have put together using several references. 

T

' Check that the active document is an assembly file
If ThisApplication.ActiveDocument.DocumentType = kAssemblyDocumentObject Then
    
    ' Define the active document As an Assembly file
    Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
	oPath = ThisDoc.Path
		
    ' Look at the files referenced by the assembly
    Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    
    For Each oRefDoc In oRefDocs
        idwPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "idw"
        ' Check to see that the model has a drawing of the same path and name
        If(System.IO.File.Exists(idwPathName)) Then
            Dim oDrawDoc1 As DrawingDocument
            oDrawDoc1 = ThisApplication.Documents.Open(idwPathName, False)
		End If
		Dim oCurrentSheet As Sheet
		' Iterate through the sheets
		Dim oSheet As Sheet
		FullFilename = ThisDoc.Path & "\drawings"& "\Panel_assy_LAVT.idw"
		Dim oDrawDoc2 As DrawingDocument = ThisApplication.Documents.Open(FullFilename)
		oDrawDoc2.Activate

		oCurrentSheet = oDrawDoc2.ActiveSheet

		For Each oSheet In oDrawDoc2.Sheets
		    oSheet.Activate
    		iLogicVb.RunExternalRule("Center_Dimensions")
			iLogicVb.RunExternalRule("Precise_View")
			iLogicVb.RunExternalRule("Sort_and_Hide_Partlist")
			iLogicVb.RunExternalRule("Update_Balloons")
			iLogicVb.RunExternalRule("View_Labels")
			iLogicVb.RunExternalRule("Center_Dimensions")
		Next
        ThisApplication.CommandManager.ControlDefinitions.Item("AppFileCloseCmd").Execute
	Next
		
Else 
    MessageBox.Show("Please run this rule from the assembly file.", "Open All Drawings from Assembly",MessageBoxButtons.OKCancel)
End If
MessageBox.Show("All Files Updated!!!", "Lazy Eye")

hank you,

0 Likes
Accepted solutions (1)
1,091 Views
3 Replies
Replies (3)
Message 2 of 4

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @bsnyderACLUW 

I don't know exactly what you need help with here... Does your code throw any errors or behaves unexpectedly?, or are you just looking for general feedback?

 

I put this together given the information about what you want to accomplish, see if it works for you 🙂

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
		Dim oDrawingPath As String = FindDrawingFile(oRefDoc)
		'Go to next if there is no drawing.
		If oDrawingPath = "" Then Continue For
		Dim oInfo As New System.IO.FileInfo(oDrawingPath)
		'Go to next if drawing is readonly (checked in for example)
		'Then you can't do much to it anyways.
		If oInfo.IsReadOnly Then Continue For
		Dim oDrawing As DrawingDocument = ThisApplication.Documents.Open(oDrawingPath, True)
		For Each oSheet As Sheet In oDrawing.Sheets
			oSheet.Activate
			iLogicVb.Automation.RunExternalRule(oDrawing, "Center_Dimensions")
			iLogicVb.Automation.RunExternalRule(oDrawing, "Precise_View")
			iLogicVb.Automation.RunExternalRule(oDrawing, "Sort_and_Hide_Partlist")
			iLogicVb.Automation.RunExternalRule(oDrawing, "Update_Balloons")
			iLogicVb.Automation.RunExternalRule(oDrawing, "View_Labels")
			iLogicVb.Automation.RunExternalRule(oDrawing, "Center_Dimensions")
		Next
		'Save and close the drawing
		oDrawing.Save
		oDrawing.Close
		ThisApplication.UserInterfaceManager.DoEvents()
	Next
End Sub
Function FindDrawingFile(PartOrAssemblyDoc As Document) As String
	Dim fullFilename As String = PartOrAssemblyDoc.FullFileName
	Dim Path As String = System.IO.Path.GetFullPath(fullFilename)
	Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(fullFilename)
	Dim drawingFilename As String
	drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(Path, filename & ".dwg")
	If drawingFilename = ""
		drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(Path, filename & ".idw")
		If drawingFilename <> "" Then
			Return drawingFilename
		Else
			Return ""
		End If
	Else
		Return drawingFilename
	End If
End Function

Keep in mind that any errors you may get could depend on your external rules aswell 🙂

Message 3 of 4

bsnyderACLUW
Enthusiast
Enthusiast

Hi @JhoelForshav ,

Thank you for your reply

sorry I guess I forgot to mention my problem lol. 

when I run my code it tries to run the external rules on the assembly causing an error.

I am trying the code you provided so far seems to work great!!! thank you!!! I am looking to take this one step further though. I am Making a form that will have check boxes related to user parameters in the document. These user parameters will be used in this code to run or skip an external rule. I think I can accomplish this with if statements for each external rule. will post later when done or stuck lol.

 

thanks,

 

0 Likes
Message 4 of 4

bsnyderACLUW
Enthusiast
Enthusiast

@JhoelForshav 

 

Thank you for Providing the code you posted. it works well and i have not found any issues with it. Though I have added to it. Now I have a Global Form that can change what external rules are ran before it runs through the drawings. Next step will to have it check for views and update bend tables. then maybe run through parts and run some functions there as well, namely DXFing the parts.  Bellow is the updated Code:

	
Sub Main
Dim oAsm As AssemblyDocument = ThisDoc.Document
If Parameter("Option1") = "True" Then
	oOption1 = "T"
Else If Parameter("Option1") = "False" Then
	oOption1 = "F"
End If 
If Parameter("Option2") = "True" Then
	oOption2 = "T"
Else If Parameter("Option2") = "False" Then
	oOption2 = "F"
End If 
If Parameter("Option3") = "True" Then
	oOption3 = "T"
Else If Parameter("Option3") = "False" Then
	oOption3 = "F"
End If 
If Parameter("Option4") = "True" Then
	oOption4 = "T"
Else If Parameter("Option4") = "False" Then
	oOption4 = "F"
End If 
If Parameter("Option5") = "True" Then
	oOption5 = "T"
Else If Parameter("Option5") = "False" Then
	oOption5 = "F"
End If 
If Parameter("Option6") = "True" Then
	oOption6 = "T"
Else If Parameter("Option6") = "False" Then
	oOption6 = "F"
End If 
If Parameter("Option7") = "True" Then
	oOption7 = "T"
Else If Parameter("Option7") = "False" Then
	oOption7 = "F"
End If 

	For Each oRefDoc As Document In oAsm.AllReferencedDocuments
		Dim oDrawingPath As String = FindDrawingFile(oRefDoc)
		'Go to next if there is no drawing.
		If oDrawingPath = "" Then Continue For
		Dim oInfo As New System.IO.FileInfo(oDrawingPath)
		'Go to next if drawing is readonly (checked in for example)
		'Then you can't do much to it anyways.
		If oInfo.IsReadOnly Then Continue For
		Dim oDrawing As DrawingDocument = ThisApplication.Documents.Open(oDrawingPath, True)
		For Each oSheet As Sheet In oDrawing.Sheets
			oSheet.Activate
		Select Case oOption1
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Center_Dimensions")
		Case "F"
			
		End Select
		Select Case oOption2
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Precise_View")
		Case "F"
			
		End Select
		Select Case oOption3
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Sort_and_Hide_Partlist")
		Case "F"
			
		End Select
		Select Case oOption4
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Update_Balloons")
		Case "F"
			
		End Select
		Select Case oOption5
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "View_Labels")
		Case "F"
			
		End Select
		Select Case oOption6
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Clear_Revisions_Single")
		Case "F"
			
		End Select
		Select Case oOption7
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Issued_For_Construction")
		Case "F"
			
		End Select
		Select Case oOption1
		Case "T"
			iLogicVb.Automation.RunExternalRule(oDrawing, "Center_Dimensions")
		Case "F"
			
		End Select

		Next
		'Save and close the drawing
		oDrawing.Save
		oDrawing.Close
		ThisApplication.UserInterfaceManager.DoEvents()
	Next
End Sub
Function FindDrawingFile(PartOrAssemblyDoc As Document) As String
	Dim fullFilename As String = PartOrAssemblyDoc.FullFileName
	Dim Path As String = System.IO.Path.GetFullPath(fullFilename)
	Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(fullFilename)
	Dim drawingFilename As String
	drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(Path, filename & ".dwg")
	If drawingFilename = ""
		drawingFilename = ThisApplication.DesignProjectManager.ResolveFile(Path, filename & ".idw")
		If drawingFilename <> "" Then
			Return drawingFilename
		Else
			Return ""
		End If
	Else
		Return drawingFilename
	End If
End Function

this is the code that creates the user parameters

Sub Main
    Dim varAssDoc As AssemblyDocument
 If ThisApplication.ActiveDocumentType = kAssemblyDocumentObject Then
        varAssDoc = ThisDoc.Document
		oMyParameter = varAssDoc.ComponentDefinition.Parameters.UserParameters
		
		'add parameters if they doesn't exist
        Try
            oParameter = oMyParameter("Option1")
        Catch
            oParameter = oMyParameter.AddByValue("Option1", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option2")
        Catch
            oParameter = oMyParameter.AddByValue("Option2", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option3")
        Catch
            oParameter = oMyParameter.AddByValue("Option3", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option4")
        Catch
            oParameter = oMyParameter.AddByValue("Option4", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option5")
        Catch
            oParameter = oMyParameter.AddByValue("Option5", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option6")
        Catch
            oParameter = oMyParameter.AddByValue("Option6", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option7")
        Catch
            oParameter = oMyParameter.AddByValue("Option7", True, "BOOLEAN")
        End Try 
		Try
            oParameter = oMyParameter("Option8")
        Catch
            oParameter = oMyParameter.AddByValue("Option8", True, "BOOLEAN")
        End Try 
    Else:
        MsgBox("Wrong Document Type", vbOKOnly, "Incorrect Document type")
    End If
End Sub

 

0 Likes