Macro to read a dxf file and extrude
Macro to read a dxf file and extrude
(OP)
I am trying to create a macro which will read a dxf file from the C drive, select the units of the dxf file as milimeter, place it into a sketch in the right plane, then extrude the sketch to 26 mm, then save the part file back on the c drive.
Sounds easy right!
When I record the macro using SW2005 starting from a blank part file (set up in mm units), everything works fine during recording and the part is created fine.
But
When I run the macro the next time, the dxf file gets loaded but the scale is 25.4 x larger than what the dxf file parameters are set up in. (It seems that the macro does not recocnize the step where the units of the dxf file are identified as milimeter). This did not happen while I was recording the macro.
In addition the macro wont extrude the sketch the way that it did when the macro was recorded?
The recorded macro is below:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
swApp.ActiveDoc.ActiveView.FrameState = 1
boolstatus = Part.Extension.SelectByID2("Right", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.FeatureManager.InsertDwgOrDxfFile "C:\Documents and Settings\Owner\My Documents\cutter.dxf"
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Right", "PLANE", 0, 0, 0, False, 32, Nothing, 0)
Part.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.026, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 1, 1, 1, 0, 0, False
Part.SelectionManager.EnableContourSelection = 0
Part.SaveAs2 "C:\Documents and Settings\Owner\My Documents\Cutter 1.SLDPRT", 0, False, False
End Sub
Also, if anyone can make a recommendaton on how to change the macro so that it will run from the blank Solidworks window without even a opening a document, I would appreaciate it.
Sounds easy right!
When I record the macro using SW2005 starting from a blank part file (set up in mm units), everything works fine during recording and the part is created fine.
But
When I run the macro the next time, the dxf file gets loaded but the scale is 25.4 x larger than what the dxf file parameters are set up in. (It seems that the macro does not recocnize the step where the units of the dxf file are identified as milimeter). This did not happen while I was recording the macro.
In addition the macro wont extrude the sketch the way that it did when the macro was recorded?
The recorded macro is below:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim FeatureData As Object
Dim Feature As Object
Dim Component As Object
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
swApp.ActiveDoc.ActiveView.FrameState = 1
boolstatus = Part.Extension.SelectByID2("Right", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.FeatureManager.InsertDwgOrDxfFile "C:\Documents and Settings\Owner\My Documents\cutter.dxf"
Part.ShowNamedView2 "*Trimetric", 8
Part.ClearSelection2 True
boolstatus = Part.Extension.SelectByID2("Right", "PLANE", 0, 0, 0, False, 32, Nothing, 0)
Part.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.026, 0.01, False, False, False, False, 0.01745329251994, 0.01745329251994, False, False, False, False, 1, 1, 1, 0, 0, False
Part.SelectionManager.EnableContourSelection = 0
Part.SaveAs2 "C:\Documents and Settings\Owner\My Documents\Cutter 1.SLDPRT", 0, False, False
End Sub
Also, if anyone can make a recommendaton on how to change the macro so that it will run from the blank Solidworks window without even a opening a document, I would appreaciate it.






RE: Macro to read a dxf file and extrude
Chris
Sr. Mechanical Designer, CAD
SolidWorks 05 SP3.1 / PDMWorks 05
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to read a dxf file and extrude
Because it is connected to another process that will need to do it about 1000 times.
Spurs
RE: Macro to read a dxf file and extrude
RE: Macro to read a dxf file and extrude
Chris
Sr. Mechanical Designer, CAD
SolidWorks 05 SP3.1 / PDMWorks 05
ctopher's home site (updated 06-21-05)
FAQ559-1100
FAQ559-716
RE: Macro to read a dxf file and extrude
Unfortunately I do not have a true DXF file (I do not use AutoCAD). I tried to mimic one by saving out a drawing as DXF. When I ran your macro, I did not have any problems with the sketch enlarging itself by 25.4. It also had no problem making the extrude. So I cannot offer any suggestions on how to fix your code. Try recording the macro again and see what happens. Do not make any more clicks than you have to.
If you record a macro that starts a blank part model you will have the code. Add it to the beginning of your existing code. You will have to do some tweaking to get this code to work consistently.
Regards,
Regg
RE: Macro to read a dxf file and extrude
When you did your test, and loaded the DXF file in during the record macro mode, did the prompt window for the dxf file ask you if the units in the file are mm or inches?
Solidworks when it creates a dxf file imposes its system of units into the dxf file which may be why you did not have the problem. According to DXF definitions, the units are not part of the dxf file, which is why Solidworks prompts for the units.
I guess the issue that I am seeing is that the macro as it executes does not prompt for the units the same way as solidworks does in the teach mode.
RE: Macro to read a dxf file and extrude
During the recording phase, the dialog came up where I could have chose units. One thing you will come to find out is that SolidWorks does not record everything, especially clicks and entries in dialog boxes.
Here is something to try. Import your DXF into SolidWorks as a drawing and then save it back out as a DXF. Now run your macro and see what happens. Also construct a simple box like I did in an inch drawing and save it out as DXF then run your macro to see what happens.
Regards,
Regg
RE: Macro to read a dxf file and extrude
If you want this to work, you will need to dig a little deeper into VBA and API, so that you can polish up the code to where it does what you want.
Do you need to autmoatically recognize and load all DXF's in a directory? Or maybe prompting for file name? This can be done, but requires a bit more coding than just recording.
You can load a DXF or DWG without creating a blank document first by using the SldWorks.LoadFile2 command. However, this creates a SLDDRW. Otherwise, you can record/code the creation of a new part file into your macro.
http://www.EsoxRepublic.com-SolidWorks API VB programming help