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")
"""