Automatic Drawings with iLogic

2018-10-05_14h36_42

I’ve written some code to automatically create drawings from Autodesk Inventor part and assembly files. The idea that I used for this blog post was to have a drawings template file set up with views, and to then simply replace these views with a given model as needed.

The GIF below shows how the code works from both parts and assemblies.

Auto Drawing

I’ve created a single rule that runs on parts, assemblies and drawings. The code identifies whether it is being run from an ipt, iam, or dwg, and then gets to work. If run from an ipt/iam, it first checks to see if a drawing exists, and if it does, it fires up that drawing. If no drawing exists, the code writes out the current part number and the full file path to a text file in C:/TEMP. The drawing template file is then copied and renamed to match the model. The drawing is then fired up and the code swaps out the reference model to match the information in our temporary text file, and saves the drawing with it’s new name. Next the user is prompted to set a scale.

Setup is pretty simple. There are 5 steps;

  • Set up an external iLogic rule called “Create Drawing”.
  • Start a drawing file of an ipt file, set the views out as you see fit.
  • Create a user parameter called “Opened”, set it to unitless (ul) and set the value to 1

Automatic drawing autodesk inventor ilogic - Clint Brown

  • Next, set up an Event trigger to run “After Open Document” and use the new “Create Drawing” external iLogic Rule.

Automatic drawing inventor ilogic - Clint Brown

  • Save the drawing file, and repeat this process for assembly files.
  • Edit the iLogic code to reflect your template file paths, note these template files should be in your project working directory.

You may be wondering what the “Opened” parameter is for, the iLogic code looks at this parameter to see if the drawing has been opened before. If it has been opened before, it exits the code, if not, it replaces the references and gives the user the option to set a scale. The parameter is then set to 2.

Here is the code, it is well commented, so should be relatively easy to follow.:

'Code By @ClintBrown3D
'Originally posted at https://clintbrown.co.uk/automatic-drawings-With-ilogic/
'Check if this is a drawing file
Dim doc = ThisDoc.Document
If doc.DocumentType = kDrawingDocumentObject Then
GoTo DRAWINGcode :
End If

'In parts & asemblies - Write file name and path to temp text file
oWrite = System.IO.File.CreateText("C:\TEMP\part.txt")
oWrite.WriteLine(ThisDoc.PathAndFileName(True))
oWrite.Close()
oFilePather = ThisDoc.Path & "\"

'In parts & asemblies - Write new drawing name to temp text file
oWrite = System.IO.File.CreateText("C:\TEMP\partno.txt")
oWrite.WriteLine(oFilePather & iProperties.Value("Project", "Part Number") & ".dwg")
oWrite.Close()

'Read Drawing name from text file
oRead = System.IO.File.OpenText("C:\TEMP\partno.txt")
EntireFile1 = oRead.ReadLine()
oRead.Close()
oDrawingName = EntireFile1

'Copy the Template file > keep templates saved in your project workspace, you need a separate part and assembly template
Dim oCopyFiler As String = "www.clintbrown.co.uk"
If doc.DocumentType = kAssemblyDocumentObject Then
oCopyFiler = "C:\Users\Clint\Workspace\ASSEMBLY_TEMPLATE.dwg"
Else If doc.DocumentType = kPartDocumentObject Then
oCopyFiler = "C:\Users\Clint\Workspace\PART_TEMPLATE.dwg"
End If

' Check if drawing exists - If it does, opening existing drawing
If System.IO.File.Exists(oDrawingName & DWGType) Then
MessageBox.Show("Drawing already exists > Opening Existing Drawing", "@ClintBrown3D")
ThisDoc.Launch(oDrawingName & DWGType)
Return
End If

'Launch New drawing
Dim oNewFiler As String = EntireFile1
System.IO.File.Copy(oCopyFiler,oNewFiler,(True))
ThisDoc.Launch(oNewFiler)

DRAWINGcode :
On Error GoTo Exiter
'Check if we have replaced the reference and scaled the drawing already
oNumbero = Parameter("Opened")
Parameter("Opened") = oNumbero + 1
'MsgBox(Parameter("Opened"))
If Parameter("Opened") > 2 Then
Return
End If

'Read in File name - For reference
oRead = System.IO.File.OpenText("C:\TEMP\part.txt")
EntireFile = oRead.ReadLine()
oRead.Close()
oPartPath = EntireFile

'Replace Drawing Reference
doc = ThisDoc.Document
Dim oFileDesc As FileDescriptor
oFileDesc = doc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFileDesc.ReplaceReference(oPartPath)
doc.Update()

'Read in new name for Drawing
oRead = System.IO.File.OpenText("C:\TEMP\partno.txt")
EntireFile1 = oRead.ReadLine()
oRead.Close()
oDrawingName = EntireFile1

'Save this drawing
ThisDoc.Save

'Scale the Drawing - Note your drawing views names("VIEW1")&("VIEW4") must match the template
On Error GoTo Exiter
oMyParameter = ThisDrawing.Document.Parameters.UserParameters
oParameter = oMyParameter.AddByValue("Scaler", "1:5", UnitsTypeEnum.kTextUnits)
MultiValue.SetList("Scaler","1:1", "1:2", "1:4", "1:5", "1:10", "1:20", "1:25", "1:50", "1:100")

Scaler = InputListBox("Set Drawing Scale", MultiValue.List("Scaler"), Scaler, Title := "Scale = " & ActiveSheet.View("VIEW1").ScaleString, ListName := "List")
ActiveSheet.View("VIEW1").ScaleString = Scaler
ActiveSheet.View("VIEW4").ScaleString = Scaler
Parameter.Param("Scaler").Delete

Exiter :
'Msgbox("Scale not Changed")

Notes:

This version of the code works with Inventor 2019 & 2020 (it should work with newer versions), it does NOT work with earlier versions (2018, 2017, 2016 etc.). I’ve put together some additional help, which you can find HERE. Please note that I cannot offer any additional support, this blog post is offered as-is, and is aimed at the more advanced user.

As always, please test all iLogic code extensively on non-production files. Do not use any code in a production environment until you have thoroughly tested it and have verified that it works as expected. Always back up any data before running any experimental code.

Comments are closed.

Create a website or blog at WordPress.com

Up ↑