Export DXF

Hello Everyone!
Please help to understand the script.
In my script does not work with the export settings options.

   Function DXFSettings()

DXFSettings = Null
Dim str1,str2,str3,str4,str5,str6,str7,str8,str9,str10
Dim str11,str12,str13,str14,str15,str16,str17,str18,str19,str20,str21,str22,str23,str24,str25,str26,str27
Dim strComb

str1 = "_ExportScheme=_CAMMetric "
str2 = "_CurrentOption=_0.05 "
str3 = "_AcadVersion=_14 "
str4 = "_MinPointDistance=_1e-06 "
str5 = "_UseMaxAngle=y "
str6 = "_UseChordHeight=y "
str7 = "_PackTextures=_No "
str8 = "_UseSegmentLength=n "
str9 = "_MaxAngle=_10 "
str10 = "_ChordHeight=0.01 "
str11 = "_SegmentLength=1 "
str12 = "_WriteMeshesAs=6 "
str13 = "_WriteSurfacesAs=5 "
str14 = "_WriteLinesAs=0 "
str15 = "_WriteArcsAs=1 "
str16 = "_WriteSplinesAs=3 "
str17 = "_WritePolylinesAs=3 "
str18 = "_WritePolycurvesAs=4"
str19 = "_Flatten=0 "
str20 = "_SplitPolycurves=n "
str21 = "_SplitSplines=n "
str22 = "_Simplify=n "
str23 = "_NoDxfHeader=n "
str24 = "_FullLayerPath=y "
str25 = "_ColorMethod=1 "
str26 = "_PreserveArcNormals=n "    
str27 = "_Enter _Enter"

strComb = str1 & str2 & str3 & str4 & str5 & str6 & str7 & str8 & str9 & str10
strComb = strComb & str11 & str12 & str13 & str14 & str15 & str16 & str17 & str18 & str19 & str22 & str21 & str22 & str23 & str24 & str25 & str26 & str27

DXFSettings = strComb

End Function


Call ExportDXF()

Sub ExportDXF()
    
    Dim arrObjs,strCurrDP,strCurrDN,strSaveDN,strFileName,strSett,filt    
    
    arrObjs = Rhino.GetObjects("Select objects to export as .dxf",, True, True)
    If Not IsArray(arrObjs) Then Exit Sub
    
    strCurrDP = Rhino.DocumentPath()
    strCurrDN = Rhino.DocumentName()
    filt = "DXF Files (*.dxf)|*.dxf||"
    
    If Not IsNull(strCurrDN) Then
        strSaveDN = Left(strCurrDN, Len(strCurrDN) - 3) & "dxf"
        strFileName = Rhino.SaveFileName("Export DXF", filt, strCurrDP, strSaveDN)
    Else
        strFileName = Rhino.SaveFileName("Export DXF", filt)
    End If
    If IsNull(strFilename) Then Exit Sub
    
    strSett = DXFSettings() 'gets your settings from the function below
    If IsNull(strSett) Then
        Call Rhino.Print("Unable to get export settings")
        Exit Sub
    End If
    Call Rhino.SelectObjects(arrObjs) 'selects your objects
    
    'runs the export using the file name/path and your settings
    Call Rhino.Command("-_Export " & chr(34) & strFileName & chr(34) & " " & strSett, False)
    
End Sub

Function DXFSettings()
    
    DXFSettings = Null
    Dim str1,str2,str3,str4,str5,str6,str7,str8,str9,str10
    Dim str11,str12,str13,str14,str15,str16,str17,str18,str19,str20,str21,str22,str23,str24,str25,str26,str27
    Dim strComb
    
    str1 = "_ExportScheme=_CAM Metric "
    str2 = "_CurrentOption=_0.05 "
    str3 = "_AcadVersion=_14 "
    str4 = "_MinPointDistance=_1e-06 "
    str5 = "_UseMaxAngle=y "
    str6 = "_UseChordHeight=y "
    str7 = "_PackTextures=_No "
    str8 = "_UseSegmentLength=n "
    str9 = "_MaxAngle=_10 "
    str10 = "_ChordHeight=0.01 "
    str11 = "_SegmentLength=1 "
    str12 = "_WriteMeshesAs=6 "
    str13 = "_WriteSurfacesAs=5 "
    str14 = "_WriteLinesAs=0 "
    str15 = "_WriteArcsAs=1 "
    str16 = "_WriteSplinesAs=3 "
    str17 = "_WritePolylinesAs=3 "
    str18 = "_WritePolycurvesAs=4"
    str19 = "_Flatten=0 "
    str20 = "_SplitPolycurves=n "
    str21 = "_SplitSplines=n "
    str22 = "_Simplify=n "
    str23 = "_NoDxfHeader=n "
    str24 = "_FullLayerPath=y "
    str25 = "_ColorMethod=1 "
    str26 = "_PreserveArcNormals=n "    
    str27 = "_Enter _Enter"
    
    strComb = str1 & str2 & str3 & str4 & str5 & str6 & str7 & str8 & str9 & str10
    strComb = strComb & str11 & str12 & str13 & str14 & str15 & str16 & str17 & str18 & str19 & str22 & str21 & str22 & str23 & str24 & str25 & str26 & str27
    
    DXFSettings = strComb
End Function

Hi Leex,

Without being able to test your curve; does it work if you add a space at the end of str18

str18 = "_WritePolycurvesAs=4"

add space after the 4

str18 = "_WritePolycurvesAs=4 "

HTH
-Willem

And here is a somewhat simpler method to assemble your string - without needing to dim and concatenate all the variables… Just dim an array of n spaces and fill that array with your string segments. Then use a For loop to concatenate… --Mitch

Option Explicit
Call TestAssembleString()
Sub TestAssembleString()
    Dim s(26), my_string,i
    s(0) = "_ExportScheme=_CAM Metric "
    s(1) = "_CurrentOption=_0.05 "
    s(2) = "_AcadVersion=_14 "
    s(3) = "_MinPointDistance=_1e-06 "
    s(4) = "_UseMaxAngle=y "
    s(5) = "_UseChordHeight=y "
    s(6) = "_PackTextures=_No "
    s(7) = "_UseSegmentLength=n "
    s(8) = "_MaxAngle=_10 "
    s(9) = "_ChordHeight=0.01 "
    s(10) = "_SegmentLength=1 "
    s(11) = "_WriteMeshesAs=6 "
    s(12) = "_WriteSurfacesAs=5 "
    s(13) = "_WriteLinesAs=0 "
    s(14) = "_WriteArcsAs=1 "
    s(15) = "_WriteSplinesAs=3 "
    s(16) = "_WritePolylinesAs=3 "
    s(17) = "_WritePolycurvesAs=4 "
    s(18) = "_Flatten=0 "
    s(19) = "_SplitPolycurves=n "
    s(20) = "_SplitSplines=n "
    s(21) = "_Simplify=n "
    s(22) = "_NoDxfHeader=n "
    s(23) = "_FullLayerPath=y "
    s(24) = "_ColorMethod=1 "
    s(25) = "_PreserveArcNormals=n "    
    s(26) = "_Enter _Enter"
    my_string = ""
    For i=0 To 26:
        my_string = my_string & s(i)
    Next
    Call Rhino.Print(my_string)

End Sub

None of the methods is not working. Curve settings do not take effect.
Export curve occurs with the default settings (inside Rhino)

   Call ExportDXF()

Sub ExportDXF()
    
    Dim arrObjs,strCurrDP,strCurrDN,strSaveDN,strFileName,strSett,filt    
    
    arrObjs = Rhino.GetObjects("Select objects to export as .dxf",, True, True)
    If Not IsArray(arrObjs) Then Exit Sub
    
    strCurrDP = Rhino.DocumentPath()
    strCurrDN = Rhino.DocumentName()
    filt = "DXF Files (*.dxf)|*.dxf||"
    
    If Not IsNull(strCurrDN) Then
        strSaveDN = Left(strCurrDN, Len(strCurrDN) - 3) & "dxf"
        strFileName = Rhino.SaveFileName("Export DXF", filt, strCurrDP, strSaveDN)
    Else
        strFileName = Rhino.SaveFileName("Export DXF", filt)
    End If
    If IsNull(strFilename) Then Exit Sub
    
    strSett = DXFSettings() 'gets your settings from the function below
    If IsNull(strSett) Then
        Call Rhino.Print("Unable to get export settings")
        Exit Sub
    End If
    Call Rhino.SelectObjects(arrObjs) 'selects your objects
    
    'runs the export using the file name/path and your settings
    Call Rhino.Command("-_Export " & chr(34) & strFileName & chr(34) & " " & strSett, False)
    
End Sub

Call TestAssembleString()
Sub TestAssembleString()
    Dim s(26), my_string,i
    s(0) = "_SegmentLength=1 "
    s(1) = "_CurrentOption=_y "
    s(2) = "_AcadVersion=_14 "
    s(3) = "_MinPointDistance=_1e-06 "
    s(4) = "_UseMaxAngle=y "
    s(5) = "_UseChordHeight=y "
    s(6) = "_PackTextures=_No "
    s(7) = "_UseSegmentLength=n "
    s(8) = "_MaxAngle=_10 "
    s(9) = "_ChordHeight=0.01 "
    s(10) = "_SegmentLength=1 "
    s(11) = "_WriteMeshesAs=7 "
    s(12) = "_WriteSurfacesAs=8 "
    s(13) = "_WriteLinesAs=4 "
    s(14) = "_WriteArcsAs=4 "
    s(15) = "_WriteSplinesAs=4 "
    s(16) = "_WritePolylinesAs=4 "
    s(17) = "_WritePolycurvesAs=4 "
    s(18) = "_Flatten=0 "
    s(19) = "_SplitPolycurves=n "
    s(20) = "_SplitSplines=n "
    s(21) = "_Simplify=n "
    s(22) = "_NoDxfHeader=n "
    s(23) = "_FullLayerPath=y "
    s(24) = "_ColorMethod=1 "
    s(25) = "_PreserveArcNormals=n "    
    s(26) = "_Enter _Enter"
    my_string = ""
    For i=0 To 26:
        my_string = my_string & s(i)
    Next
    Call Rhino.Print(my_string)

End Sub

Yes, I looked again and I’m afraid you cannot code dxf options directly at the command line like you can with, for example, .stl settings. You will need to create a custom dxf/dwg export scheme beforehand - which is stored by Rhino in the registry - and then call the scheme by name when exporting…

Sample code below… --Mitch

Option Explicit
'Script written by Mitch Heynick

Call ExportDXFwithHardCodeScheme()
Sub ExportDXFwithHardCodeScheme
    
    Dim strScheme : strScheme = "YourExportScheme" ''<-- change this as needed
    
    Dim arrObjs,strFileName,strComm    
    arrObjs=Rhino.GetObjects("Select objects to export",,True,True,True)
    If not IsArray(arrObjs) Then Exit Sub
    
    strFileName = Rhino.SaveFileName ("Export DXF", "DXF Files (*.dxf)|*.dxf||")
    If IsNull(strFileName) Then Exit Sub

    strComm="-_Export " & chr(34) & strFileName & chr(34)
    strComm=strComm&" Scheme "& chr(34) & strScheme & chr(34) & " _Enter"        
    Call Rhino.Command(strComm, False)
    
End Sub

Hi Mitch!
This is great! This is what I need.
I use three schemes for export to different programs.
Originally wanted to create three scripts with different settings within it.
Your idea was much more advanced!
Thank you so much, Mitch !!!

Hi leex,

is your script working? I am having the same issue as you.
When I use
strScheme = "R12 Natural or strScheme = "R12 Lines & Arcs"
...
[...myCode...] " Scheme " & chr(34) & strScheme & chr(34) & "_Enter"
it works perfectly fine. But how did you manage to get the WriteSurfacesAs=6 working?
If I use strScheme = "R12 Natural _WriteSurfacesAs=6 it simply ignores the second part.

As I said, you can’t access the individual settings via the command line. If you need something different than the “standard” export schemes that come with Rhino, I suggest you create a named custom scheme with your desired settings beforehand and reference that in the export script.

As being able to script these settings directly via the -Export command (as you can now with something like .stl) might be useful, I will submit a wish item for this. No idea when it might be implemented, though, I think it might be a lot of work.

http://mcneel.myjetbrains.com/youtrack/issue/RH-29210

–Mitch

Ok, I see.
So what if I create a custom AcadExportScheme ini-file.
Is it possible to call the the import function inside the AutoCAD Export Schemes dialog to change the settings?
If so how can I call it in RhinoScript?

The “Import” function in the DWG/DXF options dialog does not appear to be scriptable currently either…

–Mitch

Helvetosaur
This is exactly what I am looking for.
Now if the user export his template or modify the original I always want to force rhino to use the default value from the template polyline 2004. What is your advice?
Other problem I encountered, and really annoying thing is , that when the Language change the template polyline 2004 called with is not recognized anymore, because the language isn’t the same as before…
Ideas???

Is it possible to import, a scheme ? If polyline 2004 exists, but Rhino is in french version, Rhino doesn’t recognize Polyline 2004

With Rhino V6, you can. The ACADSchemes command allows you to manage, import and export schemes.

and last quaestion, is there a way to modify the value from the export scheme?

Well, you can edit the schemes via -_ACADSchemes (with the dash), you will have a whole bunch of command line options. It should be possible with all that to create a macro to either create a new scheme with the settings you want, or edit an existing one… I think it will need a bit of experimentation, given the number of settings… good luck.

–Mitch

Thank you a lot I will try it and give the solution

I found the solution, and gived the explaination here: