Inventor 2023 VBA changes and errors

Inventor 2023 VBA changes and errors

Frank.HallWQLC5
Enthusiast Enthusiast
522 Views
4 Replies
Message 1 of 5

Inventor 2023 VBA changes and errors

Frank.HallWQLC5
Enthusiast
Enthusiast

Hi All,

 

Just moved over to Inventor 2023 from 2021 and there are a number of VBA functions that don't seem to be working any longer. I'll try to include enough code to be self explanatory to get the point across. The purpose of this thread is to discuss the bugs and try to get to the bottom of what is intentional and what needs to be escalated. And FYI, I've been doing this kind of coding on and off since 2016 (the year not necessarily the Inventor version).

 

Starting with an easy one:

 

Dim apprentice As New Inventor.ApprenticeServerComponent
Dim apprenticeDoc As Inventor.ApprenticeServerDocument

If Right(NameAndPath, 4) = ".iam" Then
    Set apprenticeDoc = apprentice.Open(NameAndPath & "<All Components Suppressed>")

 

Level of Detail (LOD) has been removed so this bug was expected. An easy fix is to edit the code to not include & "<All Components Suppressed>" but then, is there an alternative approach to opening assemblies in a 'light' way?

 

 

A bigger issue that I'm facing and having trouble resolving is the SaveAs or Translator methods for generating export files, namely DXF, IGS & STP. Though PDF was successful. Example using the SaveAs method:

 

Dim oApp As Inventor.Application
Dim oDoc As Inventor.Document
Set oApp = CreateObject("Inventor.Application")
oApp.SilentOperation = True
Set oDoc = oApp.Documents.Open(PartNameandFilePath, False)
IgsNameandPath = [Some directory] & Worksheets("FileSet").Range("A8").Offset(Row, 0) & ".igs"
Call oDoc.SaveAs(IgsNameandPath, True)

 

I have tried using the Translator example code from Autodesk which fails on the last line: 

Call DXFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

 

0 Likes
523 Views
4 Replies
Replies (4)
Message 2 of 5

Frederick_Law
Mentor
Mentor

iLogic to export Flat to DXF:

 

Dim oModelStates As ModelStates
Dim oModelState As ModelState
Dim oDataIO As DataIO
Dim sOut As String
oDoc = ThisApplication.ActiveDocument
Logger.Info("FlatOut" & oDoc.DisplayName)
oCompDef = oDoc.ComponentDefinition
' Get the DataIO object
oDataIO= oCompDef.DataIO
' Build the string that defines the format of the DXF file
sOut = "FLAT PATTERN DXF?AcadVersion=2000&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ROLL;IV_ROLL_TANGENT&RebaseGeometry=True"

If oCompDef.IsModelStateFactory Then
	Logger.Info("ModelState in file")
	oModelStates = oDoc.ComponentDefinition.ModelStates
	For Each oModelState In oModelStates
		oModelState.Activate
		Logger.Info(oDoc.DisplayName & " " & oModelState.Name)
		Logger.Info(iProperties.Value("Project", "Part Number"))
		' Skip Primary
		If oModelState.Name <> ThisServer.LanguageTools.CurrentPrimaryModelStateString
			' Create DXF file
			oDataIO.WriteDataToFile(sOut, "\DXF\" + iProperties.Value("Project", "Part Number") + ".dxf")
		End If
	Next
Else
  Logger.Info("Normal file")
  Logger.Info(iProperties.Value("Project", "Part Number"))
  oDataIO.WriteDataToFile(sOut, "\DXF\"+iProperties.Value("Project", "Part Number")+".dxf")
End If
End Sub

 

 

Old VBA to export idw to DWG:

 

Public Sub PublishDWG()
    ' Get the DWG translator Add-In.
    Dim DWGAddIn As TranslatorAddIn
    Set DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

    'Set a reference to the active document (the document to be published).
    Dim oDocument As Document
    Set oDocument = ThisApplication.ActiveDocument

    Dim oContext As TranslationContext
    Set oContext = ThisApplication.TransientObjects.CreateTranslationContext
    oContext.Type = kFileBrowseIOMechanism

    ' Create a NameValueMap object
    Dim oOptions As NameValueMap
    Set oOptions = ThisApplication.TransientObjects.CreateNameValueMap

    ' Create a DataMedium object
    Dim oDataMedium As DataMedium
    Set oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

    ' Check whether the translator has 'SaveCopyAs' options
    If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then

        Dim strIniFile As String
        strIniFile = "C:\DWGOut.ini"
        ' Create the name-value that specifies the ini file to use.
        oOptions.value("Export_Acad_IniFile") = strIniFile
    End If

    Dim DrawingName As String
    DrawingName = " "
    Dim DrawingPath As String
    DrawingPath = " "
    Dim DrawingFullName As String
    DrawingFullName = oDocument.FullFileName
    
    Call SplitPath(DrawingFullName, DrawingPath, DrawingName)
    
    'Check if folder exist, create if not
    On Error Resume Next
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.CreateFolder(DrawingPath + "DWG")
    
    'Set the destination file name
    oDataMedium.Filename = DrawingPath + "DWG\" + DrawingName + ".dwg"

    'Publish document.
    Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
End Sub

 

 It still work in 2023.3

 

The DXF and DWG Translator work differently for some reason.

DWG allow use of ini file, DXF doesn't and need to specify all the options.

 

BTW I have the DWG export code in VB.NET for Addin also.

0 Likes
Message 3 of 5

davefosz
Contributor
Contributor

Error when trying to retrieve values from Excel:

"Dim excelApp As Excel.Application"

Not working

 

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Hi @davefosz.  When you have the VBA editor open, make sure the 'project' you are working with is selected within the Project Explorer window, then click on the Tools tab, then click on the 'References...' option in that menu.  That should open a dialog.  Within that dialog, scroll the list of references until you find one like "Microsoft Excel 16.0 Object Library" (the 16.0 part may be different for you), and put a check in the checkbox next to it.  Then click OK on that dialog, to accept the changes and close it.  That setting is project specific, which is why I included instructions to select the one you are working with beforehand.  Now any modules within that project should be able to recognize the Excel API objects.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

davefosz
Contributor
Contributor

Thanks Wesley, that fixed all my old macros that work with Excel

0 Likes