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: 

name and location in code

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
JamieSENG
1974 Views, 11 Replies

name and location in code

I found this code a while back and wanted to edit it to change the file name and the location the files save in. Currently it goes through open drawings exporting them as the file name with _acad2k saving the files in the original files location. However i would like the file name to be populated by the iproperties part number and revision and file path to be a folder on my c drive.

 

Imports SysIO = System.IO
Sub Main()
'See if there are any open views
If (ThisApplication.Views.Count > 0) Then
'Setup Translator to dwg
Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Go through each view and save if it's a drawing document
For Each view As View In ThisApplication.Views
If view.Document.DocumentType = kDrawingDocumentObject Then
'Get the directory file is saved in. Can replace this with specific directory
Dim dwgDir = SysIO.Path.GetDirectoryName(view.Document.FullFileName)
Get name Of file without the extension And add _acad2k To it.
oDataMedium.MediumType = kFileNameMedium
oDataMedium.FileName = dwgDir & "\\" & _
 SysIO.Path.GetFileNameWithoutExtension(view.Document.FullFileName) & _
 "_acad2k.dwg"

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(view.Document, oContext, oOptions) Then
'Use Export To DWG to save drawing configuration and set here
Dim strIniFile As String = "C:\Export Copy Main Directory\dwg\Format2000DONOTDELETE.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
'Save File
Call DWGAddIn.SaveCopyAs(view.Document, oContext, oOptions, oDataMedium)
End If
End If
Next
End If
End Sub

 

11 REPLIES 11
Message 2 of 12
MechMachineMan
in reply to: JamieSENG

revise this line as necessary to meet your needs:

 

oDataMedium.FileName = dwgDir & "\\" & _
 SysIO.Path.GetFileNameWithoutExtension(view.Document.FullFileName) & _
 "_acad2k.dwg"

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 12
JamieSENG
in reply to: MechMachineMan

I got the location sorted just cant get my head around the naming part

Message 4 of 12
JamieSENG
in reply to: JamieSENG

So i got the location and filename sorted. Only now when i run the code the exported files keep the first filename and overwrite one another. Could you take a look...

 

Imports SysIO = System.IO
Sub Main()
'See if there are any open views
If (ThisApplication.Views.Count > 0) Then
'Setup Translator to dwg
Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
'Go through each view and save if it's a drawing document
For Each view As View In ThisApplication.Views
If view.Document.DocumentType = kDrawingDocumentObject Then

'Get the directory file is saved in. Can replace this with specific directory
Dim dwgDir = "C:\Export Copy Main Directory\dwg\"
'Get name Of file without the extension And add _acad2k To it.
oDataMedium.MediumType = kFileNameMedium

oDataMedium.FileName = dwgDir & iProperties.Value("Project", "Part Number") & " Rev. " & iProperties.Value("Project", "Revision Number") & ".dwg"

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(view.Document, oContext, oOptions) Then
'Use Export To DWG to save drawing configuration and set here
Dim strIniFile As String = "C:\Export Copy Main Directory\dwg\Format2000DONOTDELETE.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
'Save File
Call DWGAddIn.SaveCopyAs(view.Document, oContext, oOptions, oDataMedium)
End If
End If
Next
End If
End Sub
Message 5 of 12
MechMachineMan
in reply to: JamieSENG

Your iProperties.Value call only ever grabs from the ACTIVE document. You need to DIRECT it to each document to grab that specific documents name.

 

Instead of 

iProperties.Value("Project", "Part Number")

use

 

iProperties.Value(SysIO.Path.GetFileName(view.Document), "Project", "Part Number")

 

 Good luck!

 

PS: You would have to call view.Document.Activate for every new document during the loop in which you process it, which is very time consuming to make it work with just the base iProperties.Value() call. But no one wants this, so use the info provided above instead.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 6 of 12
JamieSENG
in reply to: MechMachineMan

I realise my limitation was that the code ran through all open drawings but didn't physically go to the next, hence why I kept getting the same file throughout. For the time being, I'm just going to use the code as is. Seems more trouble than it's worth trying to fetch the iproperties.

 

You did mention before that you would need to "call view.Document.Activate for every new document during the loop"

Would this activate each drawing to apply the rule?

 

If so out of curiosity how would you go about doing it? I sense there could be an issue with the loop going back over the first?

Message 7 of 12
MechMachineMan
in reply to: JamieSENG

Imports SysIO = System.IO
Sub Main()
'Check pre-processing conditions. If (ThisApplication.Views.Count = 0) Then: Exit Sub: End if Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}") Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext oContext.Type = kFileBrowseIOMechanism
Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium
For Each oVisDoc As View In ThisApplication.Documents.VisibleDocuments If oVisDoc.DocumentType = kDrawingDocumentObject Then
DocName = SysIO.GetFileName(oVisDoc.FullFileName)
Dim dwgDir = SysIO.Path.GetDirectoryName(oVisDoc.FullFileName)
oDataMedium.MediumType = kFileNameMedium
oDataMedium.FileName = dwgDir & iProperties.Value(DocName, "Project", "Part Number") & " Rev. " & iProperties.Value(DocName, "Project", "Revision Number") & ".dwg"
If DWGAddIn.HasSaveCopyAsOptions(oVisDoc, oContext, oOptions) Then
Dim strIniFile As String = "C:\Export Copy Main Directory\dwg\Format2000DONOTDELETE.ini"
oOptions.Value("Export_Acad_IniFile") = strIniFile

Call DWGAddIn.SaveCopyAs(oVisDoc, oContext, oOptions, oDataMedium)
End If
End If
Next
End Sub

 


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 8 of 12
JamieSENG
in reply to: MechMachineMan

Ran the code an get an error on line 18: GetFileName is not a member of IO

 

Also, I noticed there isn't a location anymore with this code. So does that mean the file will save in the location of the original files?

 

 

Dim dwgDir = SysIO.Path.GetDirectoryName(oVisDoc.FullFileName)

not

Dim dwgDir = "C:\Export Copy Main Directory\dwg\"

 

Message 9 of 12
MechMachineMan
in reply to: JamieSENG

Yeah. As you have across your combined version of codes... That error is
because I forgot to type in ".Path" after SysIO.
Also feel free to change the location to whatever you desired as you have
outlined in this last reply.

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 10 of 12
JamieSENG
in reply to: MechMachineMan

Its throwing up the following error "

Error in rule: Rule0, in document: JE352-100-07TEMPL Hopper Joiner Plate.dwg

Unable to cast COM object of type 'Inventor._DocumentClass' to interface type 'Inventor.View'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70109AA3-63C1-11D2-B78B-0060B0EC020B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

 

More Info,

"System.InvalidCastException: Unable to cast COM object of type 'Inventor._DocumentClass' to interface type 'Inventor.View'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{70109AA3-63C1-11D2-B78B-0060B0EC020B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)"

 

Also, see code modified for the file path I require... have I done something wrong?

 

Imports SysIO = System.IO

Sub Main()

   'Check pre-processing conditions.
   If (ThisApplication.Views.Count = 0) Then: Exit Sub: End If

   Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
   Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
   oContext.Type = kFileBrowseIOMechanism

   Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
   Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium


   For Each oVisDoc As View In ThisApplication.Documents.VisibleDocuments
      If oVisDoc.DocumentType = kDrawingDocumentObject Then 
             DocName = SysIO.Path.GetFileName(oVisDoc.FullFileName)
             Dim dwgDir = SysIO.Path.GetDirectoryName(oVisDoc.FullFileName)
  'required path - 'Dim dwgDir = "S:\Drawing Office\Inventor Design Folder\Export Copy Main Directory - Shared\dwg\"
			 oDataMedium.MediumType = kFileNameMedium
             oDataMedium.FileName = dwgDir & iProperties.Value(DocName, "Project", "Part Number") & " Rev. " & iProperties.Value(DocName, "Project", "Revision Number") & ".dwg"

             If DWGAddIn.HasSaveCopyAsOptions(oVisDoc, oContext, oOptions) Then
                 Dim strIniFile As String = "S:\Drawing Office\Inventor Design Folder\Export Copy Main Directory - Shared\dwg\Format2000DONOTDELETE.ini"
                 oOptions.Value("Export_Acad_IniFile") = strIniFile
                 
                 Call DWGAddIn.SaveCopyAs(oVisDoc, oContext, oOptions, oDataMedium)
             End If
       End If 
    Next
End Sub

 

 

 

 

Message 11 of 12
MechMachineMan
in reply to: JamieSENG

Imports SysIO = System.IO

Sub Main()

   'Check pre-processing conditions.
   If (ThisApplication.Views.Count = 0) Then: Exit Sub: End If

   Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
   Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
   oContext.Type = kFileBrowseIOMechanism

   Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
   Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium


   For Each oVisDoc In ThisApplication.Documents.VisibleDocuments
      If oVisDoc.DocumentType = kDrawingDocumentObject Then 
             DocName = SysIO.Path.GetFileName(oVisDoc.FullFileName)
             'Dim dwgDir = SysIO.Path.GetDirectoryName(oVisDoc.FullFileName)
             Dim dwgDir = "S:\Drawing Office\Inventor Design Folder\Export Copy Main Directory - Shared\dwg\"
	     oDataMedium.MediumType = kFileNameMedium
             oDataMedium.FileName = dwgDir & iProperties.Value(DocName, "Project", "Part Number") & " Rev. " & iProperties.Value(DocName, "Project", "Revision Number") & ".dwg"

             If DWGAddIn.HasSaveCopyAsOptions(oVisDoc, oContext, oOptions) Then
                 Dim strIniFile As String = "S:\Drawing Office\Inventor Design Folder\Export Copy Main Directory - Shared\dwg\Format2000DONOTDELETE.ini"
                 oOptions.Value("Export_Acad_IniFile") = strIniFile
                 
                 Call DWGAddIn.SaveCopyAs(oVisDoc, oContext, oOptions, oDataMedium)
             End If
       End If 
    Next
End Sub

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 12 of 12
JamieSENG
in reply to: MechMachineMan

Get the following error...

 

iProperties:The document named "testdrawing.dwg" was not found.

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

Post to forums  

Autodesk Design & Make Report