Export dwg to folder using python


#1

Hello,

I’m having trouble exporting 2D lines as a dwg.
Can anyone please check out what I’m doing wrong or if there is a more elegant way to do this.

dir = rs.BrowseForFolder(message=“select forder to save output files”)
if not os.path.exists(dir):
os.mkdir(dir)

Lines2D = rs.ObjectsByLayer(“Grid”) + rs.ObjectsByLayer(“plan”) +
rs.ObjectsByLayer(“profile”) + rs.ObjectsByLayer(“front”)

rs.EnableRedraw(False)

for i in range(len(Lines2D)):
rs.Command("_SelID "+str(rs.coerceguid(Lines2D[i])))
rs.EnableRedraw(True)

rs.Command("_export " & str(os.path.join(dir, str(“File_Name”))) +
’.dwg’)

so first I’m making the user select the path folder
them I’m selecting all object from all the layers I want to export as 1 .dwg file

them I’m making a command to select everything again (maybe there is a more elegant way to do this, which will take less computing time)

and them export as .dwg which is not working at the moment.

hope someone can help me out
thanks so much


#2

There are several things, see comments in the code below…

HTH, --Mitch

import rhinoscriptsyntax as rs
import os

dir = rs.BrowseForFolder(message="select forder to save output files")
"""not sure why you need the following, as either the path dir is returned or None
if None is returned, the following will fail anyway... unless you create a valid path for dir"""
if not os.path.exists(dir):
    os.mkdir(dir)
    #change to os.mkdir(mydefaultdir) ?

#if you add the second argument to ObjectsByLayer, the objects will be selected for Export
rs.ObjectsByLayer("Grid",True)
rs.ObjectsByLayer("plan",True)
rs.ObjectsByLayer("profile",True)
rs.ObjectsByLayer("front",True)

filepath=os.path.join(dir, "TestFile")
"""You need a dash to execute the Export command without the dialog
You also need an "Enter" to accept the export scheme after and complete the command"""
rs.Command("_-Export " + filepath +'.dwg'+" _Enter")

"""If you have a specific pre-set scheme for export, you can also write -
rs.Command("_-Export " + filepath +'.dwg'+" _Scheme "+chr(34)+"Scheme Name"+chr(34)+" _Enter")
"""

#3

got it, it works…

that dash made all the diference!!! :smile:
yes you are right, that if statement doesn’t work. I need to put to save in desktop or something like that.

Thanks so much