Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Model to DXF using API

Status
Not open for further replies.

YJeepster

Mechanical
Jul 18, 2002
21
0
0
CA
I have created a code that takes the currently open Model file and creates a DXF file using info from our title block to creat the DXF file name. Now my problem that I can't seem to find away around is how do I get the longest axis of the part to be place left to right in the dxf file (IE we have a part that is 8' x 3' I want the 8' in the horizontal and the 3' in the vertical) This is for Sheet metal flat patterns.

Any ideas?
 
Replies continue below

Recommended for you

There are a couple of ways to approach this. From the sound of your post, you are starting the macro from the part file. The macro generates a drawing and then saves the drawing as dxf. Is this correct? If so, I would have the code check the model bounding box and use it to determine if the part is in the correct orientation. You could then either redefine the standard views to get the right orientation or rotate the flat pattern view after it has been created.
 
Correction. The way to do this is to check the extents of the drawing view in the x and y directions. If the x extents are less than the y extents then the drawing view needs to be rotated. The code to accomplish this in a drawing containing one view looks like this. Just run it while in a drawing - it will select the first drawing view, check its extents and rotate if required. Enjoy.

Option Explicit

Dim swApp As Object
Dim DrawingDoc As Object
Dim SelMgr As Object

Dim SheetView As Object
Dim FirstView As Object
Dim ViewOutline As Variant

Dim FirstViewName As String
Dim X_ViewExtents, Y_ViewExtents As Double

Dim BoolVal As Boolean

Const NinetyDegreesInRadians = 1.57079632679


Sub main()

Set swApp = CreateObject("SldWorks.Application")
Set DrawingDoc = swApp.ActiveDoc

Set SheetView = DrawingDoc.GetFirstView
Set FirstView = SheetView.GetNextView
ViewOutline = FirstView.GetOutline

X_ViewExtents = ViewOutline(2) - ViewOutline(0)
Y_ViewExtents = ViewOutline(3) - ViewOutline(1)

If (X_ViewExtents < Y_ViewExtents) Then
FirstViewName = FirstView.Name
BoolVal = DrawingDoc.Extension.SelectByID(FirstViewName, &quot;DRAWINGVIEW&quot;, 0, 0, 0, False, 0, Nothing)
BoolVal = DrawingDoc.DrawingViewRotate(NinetyDegreesInRadians)
End If

DrawingDoc.ClearSelection2 (True)

End Sub
 
I played with the model view extents myself, but did not think it reliable enough to use.

You notice when you first create a view, that there is a 'margin' around the view so that the view size is not the same as the part size? Well, this seems to be some amount inaccessable thru the api. Even if you manually resize the 'border' around the view, this original amount stays the same, except now the visible 'border' is not an accurate representation of what solidworks calls the border.

If your parts are of such a geometry that the X extents are quite different than the Y extents, this method may work.
But if the dimensions are &quot;close&quot; (whatever that means), and without an ability to check what SW defines as a 'margin' amount, you may end up doing it longhand:

1) get the view.
2) Use getPolyLines to get the 2d geometry out of the sketch
3) run each entity againt a hand-written &quot;bounding box&quot; routine to get the extents of each entity.
4) update a 'running tally' of the min and max entents for a final tally of the actual part extents.

The upside to this method is that, if you want to write your own dxf exporter, you can easily filter out construction-type geometry.
 
A couple other caveats you may wish to keep in mind when creating flat patterns for export:

1) The api call to create a flat pattern view returns TRUE if the VIEW is created successfully, not necessarily the FLAT PATTERN. If the underlying model has rebuild errors, or won't unfold, all you will end up with is a 2d projection of a folded part, and SW will tell you all is fine.

Call a rebuild on the part first and check the return for any errors.

2) If you are using existing flat patterns that are updated as model configurations are changed, *AND* the model configs have been changed thru the API, , *AND* you are using a derived config as a flat pattern, be aware that sometimes the flat patterns may not update. This is a bug I have passed along to SW thru my VAR.

To work around this, call the SetBendState to flatten, then rebuild, the *parent* configuration. Otherwise your flat pattern configs may lose their &quot;link to parent configuration&quot; status.
 
Rocheey & Stoker,

Thanks for the ideas and help. I have been testing this DXF Prog at home on a part and it worked out fine. **BUT** when I tried it on a few parts at work, I ran into a problem. That is on some parts it would create a DXF on the raw edge on the part that is to say for example, my DXF was a rectangle 0.077&quot; x 24.00&quot;.

Now I have looked in the Help file in SW API (Witch at the best of times I find not to much &quot;Help&quot;) We create a flat pattern veiw when we turn the part into a sheetmetal part, is there a way to copy that &quot;View&quot; into a blank drawing file at 1:1 scale?

This would serve two perposes, first the view would be of the flat the way I want ti (Not the edge of the part), second if the view didn't exist then the part is not a sheetmetal part and I could send a error to the user.

Cheers,

Geoff
 
Rocheey,

Actually this method will work regardless of whether or not the borders of the view have been manually resized. The extents are only dependant on the proportions of the part and are not affected by the size of the view border.

YJeepster,

I think that creating a named view in the part file is the best way to control the orientation of the flat pattern. You will have to manually select the orientation of the part somewhere so that the correct side is facing up anyway and it makes sense to do it in the part. I would have the macro insert a named view into a blank drawing using CreateDrawViewFromModelView() and then save out to .dxf at 1:1 scale. Using a named view in the part file also eliminates the need for the view rotation routine.

I would also code the creation of the named view. If you want to change the orientation of the part you will have to delete the existing named view using DeleteNamedView (viewname) and recreate it using NameView (viewname). It is better to have a single macro that checks for the existence of the flat pattern view, deletes it if necessary and then creates a flat pattern view. If you don't do this you will find yourself deleting and creating this view manually every time it has to change. You will definitely do this often enough to justify coding it.

Post again if you need any help with the code.
 
Stoker,

I was thinking of just using the &quot;FlatPattern&quot; view that SW creates. As we always select the &quot;Punch Side&quot; as the face, the view will have the right orientation. Secondly this view is automaticlly updated.

Thanks for the &quot;CreateDrawViewFromModelView()&quot; I will give this a try.

Thanks,
Geoff
 
Stoker,

Well I tried the &quot;CreateDrawViewFromModelView()&quot; it work, but has a limitation that you can't turn off the bend lines easily. So This is what I finially did:

bRet = swDraw.CreateFlatPatternViewFromModelView2(_swModel.GetPathName, &quot;DefaultSM-FLAT-PATTENRN&quot;,PaperWidth /2,PaperHeight /2, 0#, True)

&quot;DefaultSM-FLAT-PATTERN&quot; is the name of the flat pattern configuration created by SW when a sheet metal feature is created.

This I found work (So far) everytime, and turns off the bend lines.

Now I am tring to get my prog to force a rebuild on the model just before the dxf file is made. But I am having a little trouble. Here what I got so far:

Sub RebuildFile()

Dim ModelDoc2 As Object
Dim swApp As Object
Dim swModel As Object

Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Set swModel = swApp.ActiveDoc

swModel = ModelDoc2.Rebuild(swForceRebuildAll)

End Sub

But I get this error &quot;Object variable or With block variable not set&quot;

Am I missing somethig? I can see anything, but then again learning all this over the past couple of weeks is making my head hurt! :)

Geoff
 
I posted some source that exports dxf's to file, in a separate thread. This has its own internal exporter that does not rely on SW. This exporter will ignore the bend lines, datum points, etc... geometry only.
 
I usually use the ForceRebuild funtion to do this. This is from one of the examples in the api help file.

Dim swApp As Object
Dim Doc As Object

Sub main()

Set swApp = CreateObject(&quot;SldWorks.Application&quot;)
Set Doc = swApp.ActiveDoc

If Doc.ForceRebuild3(False) Then
MsgBox &quot;Rebuild successful&quot;
Else
MsgBox &quot;This model has rebuild errors&quot;
End If

End Sub

The syntax for the rebuild function is
void ModelDoc2.Rebuild (Options). The void means that this function does not return a value, so you would use
swModel.Rebuild(swForceRebuildAll) instead of what you had.

Of course, to use the rebuild function you will need to either import swconst.bas or declare the value of swForceRebuildAll for this line to work.


 
rocheey,

Thanks, I took a look at your thread. It seems to me that the code there pulls the dxf from a drawing. how does it handle scaling issues?

I want to be able to pull the dxf geomitry right from the model file.

I hope I was looking at the right thread. &quot;API : View to DXF module&quot;

 
>>It seems to me that the code there pulls the dxf from a drawing.

yeppers, it does. That is why I use a VIEW as a parameter.

>>how does it handle scaling issues

There ARE none. When you poll the view directly, you get &quot;full size&quot; geometry. Its just when you want to export an entire sheet (like SW DXF does) that you have to scale
each view to match your sheet size.

>>I want to be able to pull the dxf geomitry right from the model file.

So would I <grin> . Seriously tho, its pretty doable if the part has a Flat-Pattern feature, but a lot more work if its just a &quot;Process-Bends' feature. I'll probably pull that one off the shelf sometime :/
 
Rocheey,

So let see if I am getting this right. If I create a drawing view of my flat pattern (witch we do for all parts already) on our sheet templete, scale the part say to 1:5 to fit on the sheet.

Now I come a long and pull the geo, with your routeen and output it to DXF it will be at 1:1 scale so I can use it in my CAM software for tool path creation?

** or ***

do I have to create the drawing view at 1:1 scale?

Now secondly, the prog that I am making pulls info from the custom props to creat the DXF file name. Now I would have to check and see if a model is open, check to see if it was the correct model, pull the info I need then create and save the DXF.

Hmmm, I guess I could try that.

But why can't I just get the view to come in just as if I was using SW.

Insert --> Drawing view --> Named View



 
YJeepster,

You have said: &quot;I want to be able to pull the dxf geometry right from the model file&quot; and also that you already create a flat pattern drawing of the part. If you are creating a flat pattern drawing already why is it important to pull the dxf straight from the part? The dxf that you will produce is 2-D anyway, so there really is no advantage in creating it straight from the part.

The reason I ask is that it is much easier to get your dxf from a drawing view that to get it from the part file directly. The drawing view contains the 2-D information you need. In order to get a dxf straight from a part file you will need a much more complicated routine than just doing a saveas dxf on a drawing file.

You can, however create a routine that you run while in the part which will do what you require. Most of the api routines are just methods of doing in code what you are actually doing when you select commands in the SWX window. You should think of it in this way. The program you require is going to automate what you would do if you were to go through this process manually. If you were in a part and you wanted to produce a dxf of the flat pattern you would:

1) do a rebuild on the part to make sure it is up to date
2) check the name of the part file
3) open up a new drawing
4) insert the flat pattern view of your part in the correct orientation
5) save as dxf using the part file name
6) add notes to the drawing (if required)
7) save the drawing (if required)

This is how your routine will need to be structured. You can do each of these as follows:

1) rebuild - see my post June 23
2) check part name - see &quot;get model info&quot; example in the api help
3) open a new drawing - see the NewDrawing2 topic in the api help
4) insert flat pattern view - see my post June 17
5) save as dxf - you could either use the example from the api section of the swx website or use Rocheey's code. I tried Rocheey's dxf routine and it worked with no problems. A star for Rocheey. I think this routine would be a good addtion to the faq section - it is something really useful that many people won't see if it stays hidden in an old thread.
6) add notes - if you are going to print out the flat pattern view to send to the shop floor you may want to add some notes showing the bend radius, material thickness etc. You can pull this info while in the part file and then create a note in the drawing. I wrote some code to do this, or you can just do it yourself - look up SheetMetalFeatureData in the api help to see what you need to do.
7) save the drawing - see saveas in api help

If you are not going to print out the flat pattern then the scale of the flat pattern view and the sheet size really don't matter - just use a sheet at 1:1 scale and a drawing view at 1:1 scale. If you are going to print out the flat pattern then you will want to ensure that the drawing view fits within the sheet. In either case, units and scaling do matter. Notice that Rocheey's dxf routine uses a scaling variable called ConversionFactor to convert from meters to inches. Remember that internally SWX is working in meters, so when you export you have to scale the numbers. Since you are probably going to import into the CAM software in the same units that you are using in the part file, I would just query the user units while in the part file (at step 2) and set ConversionFactor to the appropriate value.

The end result of all of this would be a routine that you initiate while in the part. It will create your drawing, add the appropriate notes, save as dxf and save as SWX drawing. I think that you should be able to do all of this with only one mouse click. Keep us posted on your progress. The end result will definitely make a good addition to the faq's.

 
Stoker,

Your second list there is what I am tring to achieve. I am almost there, with the excepting of the right view coming into the DXF drawing. This is what I think is happening with the createDXFFromModelView command:

If you create a part, turn it into Sheetmetal, then step back one in the feature tree so you have the flat part on the screen, then click &quot;Front&quot; View this is what I get in my drawing.

This is not always the right view.

As the view might be the wrong side of the part (IE not the punch side we want), or if we have picked a face in sheet metal that is not collinear to the Standard &quot;Front&quot; View. This results in a DXF file of the edge of the part or an &quot;auxiliry&quot; view of the flat.

Yet if you open a new drawing, and use:

Insert -> Drawing View -> Named View

Then pick the &quot;Flat Pattern&quot; and click to place it, You get the correct view.

Oh well, I will keep playing with it and let you now the final result.
 
I think we are in the same book, and possibly on the same page <BFG>

I export literally hundreds of dxf's of flat patterns, every week. Why that code needed a 'tweak' after posting was because I ripped out the auto-rotation code, boundary recognition routines, references to nest creation, etc.

The 'user constants' could be something you could change to
DIMS and set as options on a form, etc. An option button of &quot;Metric&quot; and an option button for 'English' would toggle the 'Conversion factor' variable... you see what I mean.

Also, if you use a flat pattern, the flat pattern feature has a 'simplify geometry' parameter that 'cleans up' the geometry. If you're cutting these on a laser, maybe thats no big deal, but if you are using a tool with less 'resolution' (like a punch or plasma) you'd end up doing a lot of nibbling, etc. Plus you have the option of toggling this param on and off, depending on the machine tool they are being output for.

Prolly the easist way to get started on a batch exporter would be, thru code, create a new drawingdoc, and start a loop thru any desired ModelDoc configs, using the 'CreateFlatPatternViewFromModelView&quot; call.

Then export your dxf, erase the *second* view on the drawing sheet (first view is the sheet itself), and go back for more.





 
Status
Not open for further replies.
Back
Top