iLogic sheet metal .dxf export from assembly / EDIT: .dwg mass export
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
First of all I'd like to say I'm more of an amateur when it comes to programming and I'm using bunch of other peoples' macros and try to customize it for my needs (so it might be a messy code with some parts that probably could have been written better and differently).
I'm trying to complete my ilogic rule for exporting all flat patterns for sheet metal in an assembly. It works great, however I would love to run it in "invisible open mode" (time reasons, thats why I put a progress bar in there).
When I run it in "visible open" mode works like a charm, but when I change the parameter in line which opens the file to "False" (for open invisible) it pops up an error.
Is there any way to fix it?
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
ThisBOM.Export(“Parts only”, "Zestawienie komponentów dla blach.xls", kMicrosoftExcelFormat)
ExcelFile = ThisDoc.Path & "\Zestawienie komponentów dla blach.xls"
Dim oRefDocs As DocumentsEnumerator
Dim oRefDoc As Document
oRefDocs = oDoc.AllReferencedDocuments
'progress bar
oProgBar = ThisApplication.CreateProgressBar(False, 500, "Liczba wyeksportowanych blach")
liczba_blach=0
For Each oRefDoc In oRefDocs
Dim oCurFile As Document 'current file
'LINE WHERE THE FILE IS OPENED
oCurFile = ThisApplication.Documents.Open(oRefDoc.FullFileName, False)
If oCurFile.DocumentSubType.DocumentSubTypeID <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
'document is not sheet metal.
oCurFile.Close(True) 'True == skip save
Else
oCurFileName = oCurFile.FullFileName
Dim oDataIO As DataIO
Dim oPart As PartDocument
Dim sDXFname As String
Dim csOut As String
Dim oCompDef As SheetMetalComponentDefinition
oPart = ThisApplication.ActiveDocument
'getting right name
oPartName = oPart.FullFileName
'get the postion of the last backslash in the path
FNamePos = InStrRev(oPartName, "\", -1)
'get the file name with the file extension
FNameext = Right(oPartName, Len(oPartName) - FNamePos)
'get the file name (without extension)
FName = Left(FNameext, Len(FNameext) -4)
'get file path
FPath= Left(oPartName,Len(oPartName)-Len(FNameext))
a = GoExcel.FindRow(ExcelFile, "Sheet1", "Nazwa pliku", "=", FNameext)
komorka= "F" & a
ilosc=GoExcel.CellValue(ExcelFile,"Sheet1",komorka)
oCompDef = oPart.ComponentDefinition
oDataIO = oPart.ComponentDefinition.DataIO
gr = oCompDef.Thickness.Value*10
mat = oCompDef.Material.Name
sDXFname = FPath & "\" & gr & " mm - " & ilosc & " szt - " & mat & " - " & FName & ".DXF"
If oCompDef.HasFlatPattern = False Then
oCompDef.Unfold
Else
oCompDef.FlatPattern.Edit
End If
'csOut = "FLAT PATTERN DXF?TangentLayer=Tangents&SimplifySplines=True"
'csOut = "FLAT PATTERN DXF?AcadVersion=2000&BendUpLayer=IV_BEND&BendUpLayerLineType=37634&BendUpLayerColor=255;255;0&BendDownLayerLineType=37634"
csOut = "FLAT PATTERN DXF?AcadVersion=R12" _
+ "&OuterProfileLayer=IV_OUTER_PROFILE" _
+ "&InteriorProfilesLayer=IV_INTERIOR_PROFILES" _
+ "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_TOOL_CENTER;IV_TOOL_CENTER_DOWN;IV_ARC_CENTERS;IV_FEATURE_PROFILES;IV_FEATURE_PROFILES_DOWN;IV_ALTREP_FRONT;IV_ALTREP_BACK;IV_UNCONSUMED_SKETCHES;IV_ROLL_TANGENT;IV_ROLL"
oDataIO.WriteDataToFile(csOut, sDXFname)
oCompDef.FlatPattern.ExitEdit
oCurFile.Close(False) 'True == skip save
'progress bar
liczba_blach=liczba_blach+1
oProgBar.Message = "Wyeksportowano " + CStr(liczba_blach) + " blachy"
oProgBar.UpdateProgress
End If
Next
oProgBar.Close
MsgBox("Wyeksportowano " + CStr(liczba_blach) + " blachy")
EDIT:
Ok, I think it has to do something with part, when I call for:
oPart=ThisApplication.ActiveDocument
as it's not active when invisibly open...
I change that part for
oPart=oCurFile
and runs great.
However...
I've got the same problem with my mass .dwg export macro where I get file name from dialog box
Is there any way to define document variable when I have string name?
Apart from opening it and using "ActiveDocuments".
'dialog
Dim oFileDlg As Inventor.FileDialog = Nothing
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.MultiSelectEnabled = True
oFileDlg.Filter = "Inventor Files (*.idw)|*.idw|All Files (*.*)|*.*"
'oFileDlg.Filter = "XML Files (*.xml)|*.xml"
'oFileDlg.Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx"
'oFileDlg.Filter = "Text Files (*.txt;*.csv)|*.txt;*.csv"
'oFileDlg.Filter = "SAT Files (*.sat)|*.sat"
'oFileDlg.Filter = "IGES Files (*.igs)|*.igs"
'oFileDlg.Filter = "Step Files (*.stp)|*.stp"
'oFileDlg.DialogTitle = "Select a File"
oFileDlg.InitialDirectory = ThisDoc.Path
oFileDlg.CancelError = True
On Error Resume Next
oFileDlg.ShowOpen()
'after chosing
If Err.Number <> 0 Then
MessageBox.Show("File not chosen.", "Dialog Cancellation")
ElseIf oFileDlg.FileName <> "" Then
calosc = oFileDlg.FileName
'iteration count
'What do you want to count?
countTxt = "|"
'What do you want to analyze?
myTxt = calosc
'Count how many occurrences there are
count = (Len(myTxt) -Len(Replace(myTxt, countTxt, ""))) / Len(countTxt)
Dim i As Integer
For i=1 To count
'file
pozycja = InStr(calosc, "|")
pierwszy = Left(calosc, pozycja - 1)
ThisApplication.Documents.Open(pierwszy)
'for last
1 :
If wyjscie = True Then
pierwszy=calosc
End If
'export
' Get the DXF translator Add-In.
Dim DWGAddIn As TranslatorAddIn
DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC4-122E-11D5-8E91-0010B541CD80}")
'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument
Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
' Check whether the translator has 'SaveCopyAs' options
Dim strIniFile As String
strIniFile = "C:\Users\Public\Documents\Autodesk\Inventor 2021\Templates\pl-PL\zapis dxf.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
'Set the destination file name
utnij=Len(pierwszy)-4
pierwszy_nazwa_dwg = Left(pierwszy, utnij)
oDataMedium.FileName = pierwszy_nazwa_dwg & ".dwg" 'same folder, or uncomment:
'oDataMedium.FileName = "C:\myDXFfolder\" & ThisDoc.FileName(False) & ".dxf" 'fixed folder
'Publish document.
DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)
oDocument.Close
'after last exit
If wyjscie = True Then
GoTo 2:
End If
'new name
calosc = calosc.Substring(pozycja)
'for last
If i = count Then
wyjscie = True
ThisApplication.Documents.Open(calosc)
GoTo 1:
End If
Next
2:
End If