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: 

vba programming in inventor

14 REPLIES 14
Reply
Message 1 of 15
627mathieu
2026 Views, 14 Replies

vba programming in inventor

Hi,
I would like information on the vba programming in inventor. I would to create a macro for saving my .idw in .dwg.
Thanks
Mathieu Grondines
Mechanical designer
14 REPLIES 14
Message 2 of 15
Anonymous
in reply to: 627mathieu

Paste the following in the VBA editor and run it. Make sure to have the idw
document open and active.

Sanjay-

Sub SaveAsDWG()

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Call oDoc.SaveAsInventorDWG("C:\temp\MyInventorDWG.dwg", True)

End Sub
Message 3 of 15
sam_83
in reply to: 627mathieu

Sanjai

Save idw as dwg
It is possible to do this with Apprendice.

if it is not possible, how could we open Inventor in no visible mode to do the same?. We also need to open only the idw document without any other reference files.
Message 4 of 15
WHTAM
in reply to: 627mathieu

Not possible with apprentice, inside apprentice there's only apprenticeserverdocument type, there's no drawing document.

Set the drawing file to Defer update = True?
Message 5 of 15
sam_83
in reply to: 627mathieu

Thanks for your response whtam.
Our problem is derived of having choosen idw as idoneal for drawings. In certains departments need to copy the geometry to send to final clients. And form idw format we have less portability to other applications. In this deparments there are no need of having inventor applications.

Defer update = True ? its the same as have both formats idw and dwg of the documents?.

We think to do this only when it's needed. We are trying to open the idw drawing without it's the reference files parts/assembiles and export it to dwg format.
Is there a way to ignore all messages of not found files?

Sorry for my english.
Message 6 of 15
WHTAM
in reply to: 627mathieu

To hide the drawing file, you can use this
oDoc=ThisApplication.Documents.Open("C:\yourdrawing.dwg",False)

To defer updates (wont check for linked parts)
set oDoc.PropertySets.Item("Design Tracking Properties").Item("Defer Updates").Value = True
Message 7 of 15
Anonymous
in reply to: 627mathieu

If you're using VBA you can't open Inventor in an invisible mode because by
the time your VBA program runs, Inventor already has to be running.
However, you can start Inventor from an EXE program you've written or from
VBA in another application (i.e. Excel). You can use the CreateObject
VB/VBA function to start Inventor. By default this function starts Inventor
invisibly. If you want it visible you have to set the Visible property of
the Inventor Application object to True.

Here's a small sample that demonstrates this.


Public Sub StartInventorSample()

Dim oApp As Inventor.Application



On Error Resume Next

' Attempt to start Inventor.

Set oApp = CreateObject("Inventor.Application")

If Err Then

' Failed to start Inventor.

MsgBox "Could not start Inventor."

Exit Sub

Else

' Make the application visible.

oApp.Visible = True

End If


MsgBox oApp.Caption

End Sub



--

Brian Ekins

Autodesk Inventor API
Message 8 of 15
sam_83
in reply to: 627mathieu

Tanks again whtam

Only without set it's working in both Inventor and Apprendice

oDoc.PropertySets.Item("Design Tracking Properties").Item("Defer Updates").Value = True

The bad news is that this not prevent the File Dialog firing asking for the references files of the parts used in the drawing.
This must be answer by user iteration. That's a pitty!
Message 9 of 15
sam_83
in reply to: 627mathieu

Thanks Brian

I now able to open the idw in Invento through VB
and save it as dwg, only user iteration is needed to ignore the File Dialog for idw included parts/assemblies location.

Sub Main()
Dim oInvApp As Inventor.Application
'Launch Inventor
Set oInvApp = CreateObject("Inventor.Application")
oInvApp.Visible = True
'Set Startup options.
oInvApp.GeneralOptions.ShowStartupDialog = False

Dim oDoc As Inventor.DrawingDocument
Set oDoc = oInvApp.Documents.Open(sDestino)

' User iteration is needed in this point

Dim addIns As ApplicationAddIns
Set addIns = oDoc.Parent.ApplicationAddIns
Dim dwgAddIn As TranslatorAddIn
Dim i As Integer
For i = 1 To addIns.Count
If addIns(i).AddInType = kTranslationApplicationAddIn Then
If addIns(i).Description Like "*DWG*" Then
Set dwgAddIn = addIns.Item(i)
Exit For
End If
End If
Next i
'Activate AddIns
dwgAddIn.Activate
Dim fNAME As String
fNAME = oDoc.FullFileName
fNAME = Left(fNAME, Len(fNAME) - 3) & "dwg"
Dim iPath As Integer
Dim sPath As String
iPath = InStrRev(fNAME, "\")
sPath = Left(fNAME, iPath)
Call createDWG(oDoc.Parent, dwgAddIn, fNAME, sPath)
oDoc.Close SaveChanges = False
oInvApp.Quit
Set oInvApp = Nothing
End If
End Sub

Private Sub createDWG(oApp As Object, dwgAddIn As TranslatorAddIn, fNAME As String, sPath As String)

Dim map As NameValueMap
Dim context As TranslationContext
Dim trans As TransientObjects
Set trans = oApp.TransientObjects
Set map = trans.CreateNameValueMap
Set context = trans.CreateTranslationContext
context.Type = kFileBrowseIOMechanism
Dim b As Boolean
Dim file As DataMedium
Set file = trans.CreateDataMedium

b = dwgAddIn.HasSaveCopyAsOptions(file, context, map)

file.FileName = fNAME
'specify ini file from where the setting will be pickup

map.Value("Export_Acad_IniFile") = sPath & "dwgconfig.ini"
dwgAddIn.SaveCopyAs oApp.ActiveDocument, context, map, file

End Sub
Message 10 of 15
Anonymous
in reply to: 627mathieu

Setting the Application.SilentOperation to True should stop the dialog from
appearing and allow your process to continue with user interaction.
--
Brian Ekins
Autodesk Inventor API
Message 11 of 15
sam_83
in reply to: 627mathieu

Brian thanks!!!!
with Application.SilentOperation I can run the program without any user iteration, and that was my proposal.
You are great
Message 12 of 15
sam_83
in reply to: 627mathieu

Final working code
obejct: idw save as dwg having accesible only the idw file.

Sub Main()
Dim oInvApp As Inventor.Application
'Launch Inventor
Set oInvApp = CreateObject("Inventor.Application")
oInvApp.Visible = True
oInvApp.SilentOperation = True
'Set Startup options.
oInvApp.GeneralOptions.ShowStartupDialog = False

Dim oDoc As Inventor.DrawingDocument
Set oDoc = oInvApp.Documents.Open("C:\DrawingFile.idw")

Dim addIns As ApplicationAddIns
Set addIns = oDoc.Parent.ApplicationAddIns
Dim dwgAddIn As TranslatorAddIn
Dim i As Integer
For i = 1 To addIns.Count
If addIns(i).AddInType = kTranslationApplicationAddIn Then
If addIns(i).Description Like "*DWG*" Then
Set dwgAddIn = addIns.Item(i)
Exit For
End If
End If
Next i
'Activate AddIns
dwgAddIn.Activate
Dim fNAME As String
fNAME = oDoc.FullFileName
fNAME = Left(fNAME, Len(fNAME) - 3) & "dwg"
Dim iPath As Integer
Dim sPath As String
iPath = InStrRev(fNAME, "\")
sPath = Left(fNAME, iPath)
Call createDWG(oDoc.Parent, dwgAddIn, fNAME, sPath)
oDoc.Close SaveChanges = False
oInvApp.Quit
Set oInvApp = Nothing

End Sub

Private Sub createDWG(oApp As Object, dwgAddIn As TranslatorAddIn, fNAME As String, sPath As String)

Dim map As NameValueMap
Dim context As TranslationContext
Dim trans As TransientObjects
Set trans = oApp.TransientObjects
Set map = trans.CreateNameValueMap
Set context = trans.CreateTranslationContext
context.Type = kFileBrowseIOMechanism
Dim b As Boolean
Dim file As DataMedium
Set file = trans.CreateDataMedium

b = dwgAddIn.HasSaveCopyAsOptions(file, context, map)

file.FileName = fNAME
'specify ini file from where the setting will be pickup

map.Value("Export_Acad_IniFile") = sPath & "dwgconfig.ini"
dwgAddIn.SaveCopyAs oApp.ActiveDocument, context, map, file

End Sub
Message 13 of 15

is there a way to plot the drawing as well to a pdf?

what im looking to do is have a list of files in excell and have inventor open the file, plot it to a pdf then close the file.

is this even possible?
Message 14 of 15
petercharles
in reply to: 627mathieu

Here I am looking through the threads in the forum and opened this one.

PITY THE POOR SOD WHO'S TRYING TO MAKE SENSE OF THE CODE POSTED HERE!!
Message 15 of 15
Anonymous
in reply to: 627mathieu


Are you using Inventor 2009? 

 

Open the VBA editor in Excel and for the project
associated with the Excel document that has the file list, copy the code below
into the "ThisWorkbook" module.

 

Run the "Tools -> References" command and click
the check box next to "Autodesk Inventor Object Library" and click
OK.

 

Have Inventor open and run the macro.

 

Public Sub CreatePDFFiles()
    '
Connect to Inventor.
    Dim oInventorApp As
Inventor.Application
    On Error Resume
Next
    Set oInventorApp = GetObject(,
"Inventor.Application")
    If Err
Then
        MsgBox "Inventor must be
running."
        Exit
Sub
    End If
    On Error GoTo
0
   
    ' Get the sheet containing the
filename.
    ' You can change the name to the actual sheet in
your case.
    Dim oSheet As
Excel.Worksheet
    Set oSheet =
ThisWorkbook.Sheets("Sheet1")
   
    '
Iterate over the list of names.  This assumes they are
in
    ' column A and start in row 1, but could be edited to
handle
    ' other cases.
    Dim bContinue
As Boolean
    bContinue = True
    Dim iRow
As Integer
    iRow = 1
    Dim iColumn As
Integer
    iColumn = 1
   
Do
        Dim strFilename As
String
        strFilename =
oSheet.Cells(iRow, iColumn)
       

        ' Check that we got back
something.  If it's empty
        '
then assume we've processed the entire
list.
        If strFilename <> ""
Then
            '
Check that it's a valid filename and
process
            '
it.  Otherwise skip
it.
            If
Dir(strFilename) <> ""
Then
               
Dim oDoc As
DrawingDocument
               
Set oDoc =
oInventorApp.Documents.Open(strFilename)
               

               
Dim strPDFFilename As
String
               
strPDFFilename = Left$(strFilename, Len(strFilename) - 4) &
".pdf"
               
Call oDoc.SaveAs(strPDFFilename,
True)
               
oDoc.Close
True
            End
If
           

            iRow =
iRow + 1
       
Else
           
bContinue = False
        End
If
    Loop While bContinue
End Sub



style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

 
is there a way to plot the drawing as well to a pdf? what im
looking to do is have a list of files in excell and have inventor open the
file, plot it to a pdf then close the file. is this even
possible?

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

Post to forums  

Autodesk Design & Make Report