[ILOGIC] Automatic drawing for each part in folder

[ILOGIC] Automatic drawing for each part in folder

Anonymous
Not applicable
2,494 Views
1 Reply
Message 1 of 2

[ILOGIC] Automatic drawing for each part in folder

Anonymous
Not applicable

I'm looking for the way to make automatic drawings for parts in assembly. Not for all, only for part in the same folder, because I have particular template for this parts. The best would be if drawings will be in the same .idw file, each on a separate sheets.

 

Now I have this:

 

Imports Inventor.ViewOrientationTypeEnum
Imports Inventor.DrawingViewStyleEnum

Dim oDrawDoc as DrawingDocument    
Dim oPartDoc as Document
Dim oSheet As sheet
Dim oTG As TransientGeometry
Dim oBaseView As DrawingView

ViewScale = 1/10

'Ask To create drawing?
dwgQuery=MsgBox("Czy chcesz stworzyć rysunek tego modelu?", vbYesNo,"Drawing Selection")

If dwgQuery = vbYes Then
    oPartDoc = ThisDoc.Document
    
    'Define IDW Template File Location
    oDrawDoc = ThisApplication.Documents.Add(kDrawingDocumentObject, "C:\Users\Public\Documents\Autodesk\Inventor 2017\Templates\Nogi.idw", True)
    oSheet = oDrawDoc.Sheets.Item(1)
    
    'Define 2d view bottom Left corner points For four views
    oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(5, 25)
    oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(12, 25)
	oPoint3 = ThisApplication.TransientGeometry.CreatePoint2d(19, 25)
	oPoint4 = ThisApplication.TransientGeometry.CreatePoint2d(26, 25)
    
	oBaseView = oSheet.DrawingViews.AddBaseView(oPartDoc,oPoint1, ViewScale ,kFrontViewOrientation, kHiddenLineRemovedDrawingViewStyle, "Default")
	oBaseView = oSheet.DrawingViews.AddBaseView(oPartDoc,oPoint2, ViewScale ,kRightViewOrientation, kHiddenLineRemovedDrawingViewStyle, "Default")
	oBaseView = oSheet.DrawingViews.AddBaseView(oPartDoc,oPoint3, ViewScale ,kBackViewOrientation, kHiddenLineRemovedDrawingViewStyle, "Default")
	oBaseView = oSheet.DrawingViews.AddBaseView(oPartDoc,oPoint4, ViewScale ,kLeftViewOrientation, kHiddenLineRemovedDrawingViewStyle, "Default")	
	
	
End If

 It creates drawing for part with I have an open. 

 

Sometimes I have 40 parts and this is really time-consuming to make drawings for all parts

 

 

My assembly looks that:

 

inventor.PNG

In folder "Nogi" I have parts to make drawings. In folder "Elementy" I have other parts, I don't need that drawings.























Is there any possibility to make this from Ilogic program from main Assembly?

0 Likes
Accepted solutions (1)
2,495 Views
1 Reply
Reply (1)
Message 2 of 2

AlexFielder
Advisor
Advisor
Accepted solution

Hi @Anonymous,

 

Below you will see my implementation of a solution for this problem:

Spoiler

 

 

Imports Inventor.ViewOrientationTypeEnum
Imports Inventor.DrawingViewStyleEnum
Imports System.IO

Public Sub Main

createDrawingsFromAssembly()
'CreateDrawingsFromAssembly(ThisDoc.Document)
End Sub
Public Sub createDrawingsFromAssembly()
    ' Set reference to active document.
    ' This assumes the active document is an assembly
    Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
    
	
    ' Get assembly component definition
    
	If TypeOf ThisApplication.ActiveDocument is AssemblyDocument Then
		dim oAssyDoc as AssemblyDocument = oDoc
		Dim oCompDef As Inventor.ComponentDefinition
		oCompDef = oAssyDoc.ComponentDefinition
		Dim oSubDoc As Inventor.Document
    	' Get all occurrences from component definition for Assembly document
    	Dim oCompOcc As ComponentOccurrence
    	For Each oCompOcc In oCompDef.Occurrences
        	' Check if it's child occurrence (leaf node)
        	If oCompOcc.SubOccurrences.Count = 0 Then
				'PART!
				oSubDoc = CType(oCompOcc.Definition.Document, Document)
				BeginCreateDrawing(oSubDoc)
        	Else
				'ASSEMBLY!
            	oSubDoc = CType(oCompOcc.Definition.Document, Document)
				BeginCreateDrawing(oSubDoc)
            	Call processAllSubOcc(oCompOcc) ' subassembly
        	End If
    	Next
	Else If TypeOf ThisApplication.ActiveDocument is PartDocument Then
		dim partDoc as PartDocument = oDoc
		BeginCreateDrawing(partDoc)
    End If
	MessageBox.Show("Done Creating Drawings!","You lucky thing you!")
End Sub

Public Function GetRootFolder(ByVal path As String) As String
Dim filepath As String = path
Dim directoryName As String 
Dim i As Integer = 0

While i < 2
'While filepath <> Nothing
    directoryName = System.IO.Path.GetDirectoryName(filepath)
	'MessageBox.Show(directoryName, "GetRootFolder")

    'Console.WriteLine("GetDirectoryName('{0}') returns '{1}'", filepath, directoryName)
    filepath = directoryName
    If i = 1
       filepath = directoryName + "\"  ' this will preserve the previous path
    End If
    i = i + 1
End While
Return filepath
End Function
' This function is called for processing sub assembly.  It is called recursively
' to iterate through the entire assembly tree.
Private Sub processAllSubOcc(ByVal oCompOcc As ComponentOccurrence)
   
    Dim oSubCompOcc As ComponentOccurrence
    For Each oSubCompOcc In oCompOcc.SubOccurrences
        ' Check if it's child occurrence (leaf node)
        If oSubCompOcc.SubOccurrences.Count = 0 Then
            'PART!
			oSubDoc = CType(oSubCompOcc.Definition.Document, Document)
			BeginCreateDrawing(oSubDoc)
        Else
            oSubDoc = CType(oSubCompOcc.Definition.Document, Document)
			BeginCreateDrawing(oSubDoc)
            Call processAllSubOcc(oSubCompOcc)
        End If
    Next
End Sub

Private Sub BeginCreateDrawing(ByVal oDoc as Inventor.Document)
	'Dim projectRootFolder= GetRootFolder(oDoc.FullFileName)
	Dim projectRootFolder= System.IO.Path.GetDirectoryName(oDoc.FullFileName)
	Dim tmpfile As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullFileName)
	Dim tmpDrawingName As String = projectRootFolder & "\Drgs\" & tmpfile & ".dwg"
	'Dim tmpDrawingName As String = projectRootFolder & "Drgs\" & tmpfile & ".dwg"
	'MessageBox.Show(tmpDrawingName, "tmpDrawingName")
	If Not projectRootFolder.Contains("Content Center Files") Then
    	If Not System.IO.File.Exists(tmpDrawingName) = True Then
			'make a new drawing!
			'MessageBox.Show("No existing drawing found, creating one now!", "Go Gadget go!")
			CreateDrawing(oDoc,tmpDrawingName)
		Else
			Dim tmpDrawDoc As DrawingDocument
			UpdateStatusBar("Opening existing drawing: " & tmpDrawingName) 
			tmpDrawDoc = ThisApplication.Documents.Open(tmpDrawingName, True)
			'MessageBox.Show("Existing Drawing found: "& tmpDrawingName, "Title")
		End If
	End If

End Sub
Private Sub CreateDrawing(ByVal oDoc as Inventor.Document,ByVal DrawingName As String)
	UpdateStatusBar(DrawingName) 
	Dim oBaseView As DrawingView
	Dim oView1 as DrawingView
	Dim oView2 as DrawingView
	Dim oView3 as DrawingView
	Dim oView4 as DrawingView 

	Dim ViewScale As Double = 1/10
	Dim NodeName() As String
	Dim InstNum As String

	'Define dwg Template File Location
	Dim oDrawingDoc as DrawingDocument = ThisApplication.Documents.Add(DocumentTypeEnum.kDrawingDocumentObject, "C:\Users\Public\Documents\Autodesk\Inventor 2017\Templates\APS Standard.dwg", True)
	Dim oSheet As sheet = oDrawingDoc.Sheets.Item(1)

	'Define 2d view bottom left corner points for four views
	oPoint1 = ThisApplication.TransientGeometry.CreatePoint2d(11, 11) 'BASE
	oPoint2 = ThisApplication.TransientGeometry.CreatePoint2d(28, 11) 'RIGHT
	oPoint3 = ThisApplication.TransientGeometry.CreatePoint2d(11, 20) 'TOP
	oPoint4 = ThisApplication.TransientGeometry.CreatePoint2d(28, 20) 'ISOMETRIC
	'ISOMETRIC?
	'view styles can be: 
	'DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle
	'DrawingViewStyleEnum.kHiddenLineDrawingViewStyle
	'DrawingViewStyleEnum.kShadedDrawingViewStyle
	'DrawingViewStyleEnum.kShadedHiddenLineDrawingViewStyle
	'DrawingViewStyleEnum.kFromBaseDrawingViewStyle
	'
	
	oBaseView = oSheet.DrawingViews.AddBaseView(oDoc,oPoint1, ViewScale,kFrontViewOrientation, DrawingViewStyleEnum.kHiddenLineRemovedDrawingViewStyle)', KTANGENTEDGESON)

	oView2 = oSheet.DrawingViews.AddProjectedView(oBaseView,oPoint2, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
	oView3 = oSheet.DrawingViews.AddProjectedView(oBaseView,oPoint3, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
	oView4 = oSheet.DrawingViews.AddProjectedView(oBaseView,oPoint4, DrawingViewStyleEnum.kFromBaseDrawingViewStyle)
	oDrawingDoc.SaveAs(DrawingName,False)
	'synchronize drawing properties to the model it's based upon:
	'these next lines don't work because the code needs to be run from the drawing itself!
	'iLogicVb.RunExternalRule("C:\VAULT WORKING FOLDER\Designs\Synchronize iProperties between parts and drawings.iLogicVb")
	'oDrawingDoc.Save()
End Sub

''' <summary>
''' updates the statusbar with a string value.
''' </summary>
''' <param name="Message"></param>
''' <remarks></remarks>
Private Sub UpdateStatusBar(ByVal Message As String)
	ThisApplication.StatusBarText = Message
End Sub

''' <summary>
''' Updates the statusbar with a percentage value
''' </summary>
''' <param name="percent"></param>
''' <param name="Message"></param>
''' <remarks></remarks>
Private Sub UpdateStatusBar(ByVal percent As Double, ByVal Message As String)
	ThisApplication.StatusBarText = Message + " (" + percent.ToString("P1") + ")"
End Sub

 

 

I notice that we have both used the same "CreateDrawings" method; the only real difference with mine is that I can run this from an assembly file and have it create new or open existing drawings for every part or assembly occurrence that it finds.

 

The issue you will likely have with it is that in order to run it on your assembly with the folder structure you have set up, you will need to iterate through the Browser nodes getting the document object for each node within the relevant folder(s).

 

This iLogic rule of mine shows you how to loop through the BrowserNodes although you might find it easier to simply place the parts in specific folders within your windows explorer folder structure and then look for and exclude files whose paths don't match like this:

Spoiler

 

 

If Not subdoc.File.FullFileName.Contains("Content") Then 'skip CC and FACILITY files
If Not subdoc.File.fullfilename.contains("FACILITY") Then
updatestatusbar(percent, "Processing: " & System.IO.Path.GetFileNameWithoutExtension(subdoc.File.fullfilename))
Dim oSketchBlocks As SketchBlockDefinitions = subdoc.ComponentDefinition.SketchBlockDefinitions
If oSketchBlocks.Count > 0 Then
SetorCreateCustomiProperty(subdoc, "SKETCHBLOCKPRESENT", True)
'iProperties.Value("Custom", "SKETCHBLOCKPRESENT") = True
SetorCreateCustomiProperty(subdoc, "SKETCHBLOCKCOUNT", oSketchBlocks.Count.ToString())
'iProperties.Value("Custom", "NUMSKETCHBLOCKS") = oSketchBlocks.Count
End If
End If
End If


FWIW: Wrapping code in spoilers prevent wall-of-text-posts to anyone who happens to click into the reply and isn't interested in the code.

 

Hope this helps!

 

Regards and Thanks,

 

Alex.