iLogic sheet metal .dxf export from assembly / EDIT: .dwg mass export

iLogic sheet metal .dxf export from assembly / EDIT: .dwg mass export

Slakaz
Participant Participant
638 Views
2 Replies
Message 1 of 3

iLogic sheet metal .dxf export from assembly / EDIT: .dwg mass export

Slakaz
Participant
Participant

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.

Slakaz_0-1618559565789.png

 

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

 

 

0 Likes
639 Views
2 Replies
Replies (2)
Message 2 of 3

Slakaz
Participant
Participant

UPDATE:

Yeah, that .dxf macro seem to fail in bigger assemblies. 😕

0 Likes
Message 3 of 3

Slakaz
Participant
Participant

Ok, as I can't edit earlier posts anymore I'll update You with my research outcome.

Whole "visible working"/ "invisible not working" comes to using "ActiveDocument" command, which is not working when file is open invisibly.

 

So as I reckon these two programs works when I replace "ActiveDocument". Check.

 

The problem is also within auto triggered macros inside opened files for example in part (auto updating some properties):

 

 

If iProperties.Value("Custom", "Waga wpisywana") = "" Then
iProperties.Value("Custom", "Waga")=Round(iProperties.Mass,1)&" kg"
Else
iProperties.Value("Custom", "Waga")=iProperties.Value("Custom", "Waga wpisywana")&" kg"
End If

 

or within drawing:

 

 

Try
Dim oSheets As Sheets 
oSheets = ThisDoc.Document.sheets
Dim oSheet As Sheet
For Each oSheet In oSheets
oSheet.Activate
Dim oDrawingView As DrawingView 
oDrawingView = oSheet.DrawingViews(1)
oModelName = oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument.DisplayName
oDesc = iProperties.Value(oModelName, "Project", "Part Number")
oSheet.Name =  oDesc 
Next
Catch
End Try

'automatyczne ustawianie skali z widoku_1"
For Each oSheet In ThisApplication.ActiveDocument.Sheets
    ActiveSheet = ThisDrawing.Sheet(oSheet.Name)
	If oSheet.TitleBlock Is Nothing Then Exit Sub
    oTitleBlock = oSheet.TitleBlock
    oTextBoxes = oTitleBlock.Definition.Sketch.TextBoxes
    For Each oTBox In oTextBoxes
        If oTBox.Text = "<Part Number>" Then
            oTitleBlock.SetPromptResultText(oTBox, oSheet.Name)
        End If
            If oTBox.Text = "<Skala>" Then
			Try
          	nazwa_widoku=ThisApplication.ActiveDocument.ActiveSheet.DrawingViews.item(1).Name
            oTitleBlock.SetPromptResultText(oTBox, ActiveSheet.View(nazwa_widoku).ScaleString)
            Catch
                oTitleBlock.SetPromptResultText(oTBox, " : ")
            End Try
		End If
    Next
Next

 

 

So right now I'm trying to modify these ones, so the programs I showed You in the first post would work.

When auto trigger is off, .dxf/.dwg macros seem to work without problem.

 

0 Likes