Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Automation using VBA

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
prashanth_s1997
831 Views, 8 Replies

Automation using VBA

Hello Everyone,

I'm new to using Inventor and VBA, and I'm wondering if it's possible to automate the following tasks in Inventor using VBA. If so, could someone please provide guidance on the necessary steps?

 

Task 1: I need to insert a welding sticker into 50 Inventor drawing files to indicate welding procedures.

Task 2: I need to remove the old welding procedure sticker from 50 Inventor drawing files.

Task 3: I need to update the project name and date in the title block of 50 Inventor drawing files.

 

Currently, I'm manually opening each drawing to perform these tasks, and it would be incredibly helpful if someone could point me in the right direction for automation.

Labels (2)
8 REPLIES 8
Message 2 of 9
romu51
in reply to: prashanth_s1997

For task 1, search for create and insert a sketch symbol

For Task 2, you will need the name of the sketch symbol if it was used or maybe search for text box and delete. 

 

i can help with task 3!

 

iProperties.Value("Project", "Project") = "Your Project"
iProperties.Value("Project", "Creation Date") = Now

InventorVb.DocumentUpdate()

 

Message 3 of 9

Hi @prashanth_s1997.  First of all, I would very much recommend that you start out using Inventor's iLogic, instead of Inventor's VBA, because the VBA in Inventor is on its way out.  The past few releases of Autodesk Inventor no longer include the VBA editor in the normal installations, because it has become outdated, and the folks at Microsoft have not been keeping up with security updates for it anymore, which can lead to security risks.  Inventor's iLogic is a built-in ApplicationAddIn that is geared towards folks with very little or no previous programming experience, and is based on the VB.NET programming language, which is pretty similar in many ways to VBA, but newer.

 

Next, what do you mean by 'sticker'?  Do you mean a Welding Symbol (WeldSymbol)?  If so, those have leaders usually need to be attached to some model geometry within a drawing view, and can have multiple nodes along their leaders, and have a user interface dialog which offers many ways to customize that symbol.  That could be pretty complicated to write code to place those, and position them the way you want them, and get them customized the way you want them.  For one thing, they only became available to Inventor's API (Application Programming Interface) in the 2024 release of Inventor, so if you don't have Inventor 2024, it may be impossible right now.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 9
prashanth_s1997
in reply to: romu51

Thank you

Message 5 of 9

Thank you,
By sticker I mean its just a box containing text that describes the parameters of welding
Message 6 of 9

OK.  If you want a code based solution for taking care of this task, then every little detail needs to be known about the task and all the objects involved, because code needs to be very exact.

  • I need to make sure I understand what type of object it is.
    • Is it a simple GeneralNote, placed directly on the sheet somewhere?  If so, those generally do not have a visible border around them, which makes me question whether it is within some type of drawing level sketch, instead of simply placed out on the sheet.
    • Is it a TextBox within a drawing sketch of some type, such as a sketch for a TitleBlockDefinition, or a sketch for a BorderDefinition (the border around a sheet), or a sketch for a SketchedSymbolDefinition, or a regular drawing based sketch that may belong to the sheet, or a specific drawing view.
  • Then where is it in the drawing?
    • What sheet is it on?  Or is it on all sheets?
    • If it is a TextBox within a drawing sketch, then...
      • Was the sketch for a BorderDefinition, TitleBlockDefinition, SketchedSymbolDefinition? 
        • If so, which type is it in, and what is its name?
      • If it is just in a regular drawing sketch, then does that sketch have a consistent name?...and if so what is that name?
      • If it is in a sketch, but the sketch does not have a consistent name among the drawing documents, then how else can we identify which one it is?  Maybe include a screen captured image of it and/or post its contents, so we can inspect the contents of the objects we find, in order to find the correct one.
  • How to find / identify this object among other similar objects in the drawing by code?
    • If this object does not have a name of its own, then what text does it contain, so we can check the contents of the objects to find the correct one?

Edit:  Also, are all of those 'old' drawings directly within one folder, or are they within several different folders?

If the code solution were to include a 'Browse' ability, would you prefer to pick each file individually to process, or would you prefer to pick a folder to process all the files within that folder.  And if you prefer the whole folder route, then how would we be able to know which files in that folder need to be processed, and which do not?  Is there something special about their file names, or maybe something special in their iProperties?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 9

Thank you so much @WCrihfield  for taking your time to help me:


1. It is a user defined sketch symbol (Manage ----> Define ------> Symbol). I have to insert 2 symbols as show in the below image at the left corner of all the sheets. Both stickers have their own names and have been saved under : C:\Users\Public\Documents\Autodesk\Inventor19\Design Data\Symbol Library

 

prashanth_s1997_0-1692799855733.png

2. Sticker names: WPS-ASME-B31.3-316L

                               Pipe class 1SD0P

 

 

3. The drawing files are in different folders inside a common project folder. I have to insert this in some of the .dwg files in the project folder. So, I would prefer to pick each file individually to process.

 

4. Since, I have inventor 19, is it possible to do this?

 

Thank you again for your help. Please tell me if there are any tutorials to learn automation in inventor?

Message 8 of 9

Hi @prashanth_s1997.  I think I have something ready for you to review and/or test.  Please double check the file name of the sketched symbol library file.  I assume it is a .dwg, instead of an .idw, since you mentioned that your drawing files are .dwg.  Also make sure the names of the sketched symbol definitions are correct.  Capitalization may be important too.  Anyways, this code is designed to 'update' the old sketched symbol within the old drawing files with from the sketched symbol library, which assumes that the old and new sketched symbols still have exactly the same names.  If this is not the case, then the code process will need to become more detailed and complicated.  This code does not yet contain much feedback for when something doesn't work, but does include a lot of error avoidance, so that the rest of the code can continue when something doesn't work.  All that can be further developed later.  I broke this down into 3 parts (main controlling routine, file dialog routine, and a routine for processing each file (in case multiple files were selected within the file dialog).  If you have questions about the code before trying it out, feel free to ask them, because I have not tested this code myself yet, due to the custom contents.

 

Sub Main
	'the full path & file name (with extension) of the SketchedSymbols Library drawing file
	Dim sSLibDocFile As String = "C:\Users\Public\Documents\Autodesk\Inventor19\Design Data\Symbol Library.dwg"
	'the DrawingDocument representing the SketchedSymbols Library
	Dim oSLibDoc As DrawingDocument = Nothing
	Try : oSLibDoc = ThisApplication.Documents.Open(sSLibDocFile, False) : Catch : End Try
	If oSLibDoc Is Nothing Then Exit Sub
	'while the file dialog is open, you can also select multiple files, instead of just 1
	Dim sFileSelection As String = UseFileDialog(False, "Choose Drawings To Update")
	If sFileSelection = "" Then Exit Sub
	If sFileSelection.Contains("|") Then 'multiple files were selected
		Dim oFiles As New List(Of String) 'initialize it
		oFiles.AddRange(sFileSelection.Split("|"))
		For Each sFile As String In oFiles
			UpdateSketchedSymbolsFromLibrary(sFile, oSLibDoc)
		Next
	Else
		UpdateSketchedSymbolsFromLibrary(sFileSelection, oSLibDoc)
	End If
	oSLibDoc.Close(True) ' True = skip saving the library file when closing it
	MsgBox("Process completed.", vbInformation, "iLogic")
End Sub

Function UseFileDialog(Optional bSave As Boolean = False, Optional sTitle As String = vbNullString) As String
	Dim oFDlg As Inventor.FileDialog : ThisApplication.CreateFileDialog(oFDlg)
	oFDlg.DialogTitle = IIf(sTitle = "", "Select A File.", sTitle)
	oFDlg.InitialDirectory = ThisApplication.DesignProjectManager.ActiveDesignProject.WorkspacePath
	'oFDlg.Filter = "Autodesk Inventor Files (*.iam;*.dwg;*.idw;*.ipt) | *.iam;*.dwg;*.idw;*ipt | All files (*.*)|*.*"
	oFDlg.Filter = "Autodesk Inventor Drawings (*.dwg;*.idw) | *.dwg;*.idw"
	oFDlg.MultiSelectEnabled = True : oFDlg.OptionsEnabled = False
	oFDlg.InsertMode = False : oFDlg.CancelError = True
	Try : If bSave = True Then : oFDlg.ShowSave : Else : oFDlg.ShowOpen : End If : Catch : End Try
	Return oFDlg.FileName
End Function

Sub UpdateSketchedSymbolsFromLibrary(sFileToProcess As String, oSSLibDoc As DrawingDocument)
	If String.IsNullOrEmpty(sFileToProcess ) OrElse (System.IO.File.Exists(sFileToProcess ) = False) Then Exit Sub
	If oSSLibDoc Is Nothing Then Exit Sub
	Dim oDDoc As DrawingDocument = Nothing
	Try : oDDoc = ThisApplication.Documents.Open(sFileToProcess , False) : Catch : End Try
	If oDDoc Is Nothing Then Exit Sub
	Dim oSSDs As SketchedSymbolDefinitions = oSSLibDoc.SketchedSymbolDefinitions
	Dim oWPS_SD As SketchedSymbolDefinition = Nothing
	Dim oPC_SD As SketchedSymbolDefinition = Nothing
	Try : oWPS_SD = oSSDs.Item("WPS-ASME-B31.3-316L") : Catch : End Try
	Try : oPC_SD = oSSDs.Item("Pipe class 1SDOP") : Catch : End Try
	If oWPS_SD IsNot Nothing Then
		Try : oWPS_SD.CopyTo(oDDoc, True) : Catch : End Try
	End If
	If oPC_SD IsNot Nothing Then
		Try : oPC_SD.CopyTo(oDDoc, True) : Catch : End Try
	End If
	If oDDoc.RequiresUpdate Then oDDoc.Update2(True)
	If oDDoc.Dirty Then oDDoc.Save2(False)
	oDDoc.Close(True)
End Sub

 

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

 

Edit:  I fixed the sFile vs sFileToProcess variable mismatch between the definition line of the 'UpdateSketchedSymbolsFromLibrary' Sub routine, and some of the lines of code within it.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

Hi @prashanth_s1997.  Here is a shorter version of that iLogic rule that attempts to do the same task, but tries to avoid the hard coded library file path, and attempts to get the library from each drawing file instead.  I attached it as a text file.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report