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
Solved! Go to Solution.
Solved by MechMachineMan. Go to Solution.
Solved by MechMachineMan. Go to Solution.
revise this line as necessary to meet your needs:
oDataMedium.FileName = dwgDir & "\\" & _ SysIO.Path.GetFileNameWithoutExtension(view.Document.FullFileName) & _ "_acad2k.dwg"
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
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.
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?
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
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\"
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
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
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.