Author Topic: Macro to export an OBJ file -- Updated!  (Read 4338 times)

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4225
Macro to export an OBJ file -- Updated!
« on: December 05, 2006, 12:43:04 PM »
Some time ago I created a macro to export DesignCAD surfaces (planes and grids) to an OBJ file that can be read by most other modeling programs. This can help you in the case where a particular program (Lightwave is an example) doesn't like DesignCAD's DXF output for surfaces.

Usage:
Download the macro ExportObj to your DesignCAD folder. Follow these steps to export a drawing to obj format:
1) Load the DesignCAD drawing that you want to export.
2) Tools/Macro Execute -> "ExportObj.d3m"
3) Load the obj file into the modeler of your choice.

Limitations:
1) DesignCAD pays no attention to normals, so you may have to reverse the normals on some of the faces of the exported objects before they look correct in your alternate application. Most modeling programs that care about normal orientation provide a means to reverse the direction of one or more faces of an object.

2) The macro makes no attempt to tesselate complex polygons into triangles. Some programs won't even acknowledge a facet that has more than 4 vertices. (Poser is an example that comes to mind.) For cases like this, you may have to do some intermediate adjustments in another program.

3) Different programs have different expectations of OBJ files. This exporter is about as no-frills as it gets. That means no colors, no materials. Faces are grouped first by group ID, if any, then by Solid ID, if any. All non-grouped, non-solid entities go into the same face group.

Enough with the caveats! Here's the file. This macro should work in DesignCAD 3D Max 14 and above.

NOTE: As of DesignCAD 3D Max 25, DesignCAD can now directly export obj files, and there's an option to tesselate all facets into triangles.

Update 11/17/2016 -- Updated the macro to use the existing filename and path, if the drawing has been saved.
« Last Edit: November 17, 2016, 08:29:43 AM by DrollTroll »
25 years with DesignCAD

Ignoblius

  • Newbie
  • *
  • Posts: 9
Re: Macro to export an OBJ file
« Reply #1 on: May 13, 2008, 04:16:29 PM »
You can flip normals on the mesh object in DCAD by selecting the object, calling up the info box (ctrl-I) and hitting the "Reverse the order of the points" button. It's 3rd on the top right, with a graphic of opposing red and blue arrows.

Ignoblius

  • Newbie
  • *
  • Posts: 9
Re: Macro to export an OBJ file
« Reply #2 on: May 13, 2008, 04:32:04 PM »
One problem I had with this macro is that I don't get to pick the destination of the OBJ file created-it is created in the same directory that the macro is in. I get around this problem by having a "DCAD generated OBJ" folder where I want it and placing the macro file in that folder.

Another problem is that I can only use it once in a drawing session-if I try to use it again the macro ignores any changes made to the drawing since the first time. I have to save, close and reopen the drawing. Fortunately, if I do this without exiting DCAD the "Macro Execute" menu line defaults back to my folder of choice.

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4225
Re: Macro to export an OBJ file
« Reply #3 on: May 19, 2008, 10:36:38 AM »
You can flip normals on the mesh object in DCAD by selecting the object, calling up the info box (ctrl-I) and hitting the "Reverse the order of the points" button. It's 3rd on the top right, with a graphic of opposing red and blue arrows.

Indeed you can. However, you can't really tell which way the normal is facing in DesignCAD. I suppose you could export to obj, load the obj in another program, note which normals are backwards, then pick the faces in DesignCAD and reverse them and export again. I find it much more efficient to just do the face-flipping directly in the destination program (unless it's Poser -- the grouping tools in Poser are awful).
25 years with DesignCAD

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4225
Re: Macro to export an OBJ file
« Reply #4 on: May 19, 2008, 10:53:03 AM »
Another problem is that I can only use it once in a drawing session-if I try to use it again the macro ignores any changes made to the drawing since the first time. I have to save, close and reopen the drawing.

That's quite strange -- I've not seen any problem like that. Is it possible you're overwriting an obj file that you've got open in a different application? That might cause the file to be locked and thus not get overwritten -- but I don't see in that case why closing the drawing in DesignCAD would have any effect...
25 years with DesignCAD

Ignoblius

  • Newbie
  • *
  • Posts: 9
Re: Macro to export an OBJ file
« Reply #5 on: May 21, 2008, 05:05:52 PM »
That's possible. Usually when I use the macro, I have DCAD and Poser open at the same time so I hunt for wrong-way normals in Poser and flip them in DCAD.

DrollTroll

  • Kindly Curmudgeon
  • Administrator
  • *****
  • Posts: 4225
Re: Macro to export an OBJ file -- Updated!
« Reply #6 on: March 30, 2018, 01:07:36 PM »
Just thought I'd add this note -- as of version 27 (DesignCAD 3D Max 2018), you can now fix the facing of normals inside DesignCAD, using the 'Normals' render option under OpenGL shading. You'll need to explode the solid first, choose the 'Normals' shading mode,  then select any planes or grids that render in a darker color and flip them using the Info Box's 'Reverse points' feature.
25 years with DesignCAD

Lar

  • Hero Member
  • *****
  • Posts: 2780
Re: Macro to export an OBJ file -- Updated!
« Reply #7 on: March 30, 2018, 01:47:28 PM »

You'll need to explode the solid first,

Just a tip: you don't have to explode solids if you use basiccad. The GET/PUTAttr statements can select a single entity in a solid by its id and the regular commands can manipulate that entity only.


I have a macro to assist in flipping normal but it is highly enhanced to my 3d workstyle so will require a tutorial to explain to others.


Lar
« Last Edit: March 30, 2018, 01:51:18 PM by Lar »

Dr PR

  • Hero Member
  • *****
  • Posts: 5588
Re: Macro to export an OBJ file -- Updated!
« Reply #8 on: March 30, 2018, 03:07:28 PM »
Lar,

Does you macro detect reversed normals, of is it necessary to cycle through polygons to find those with reversed normals?

Phil
DesignCAD user since 1987

Lar

  • Hero Member
  • *****
  • Posts: 2780
Re: Macro to export an OBJ file -- Updated!
« Reply #9 on: March 30, 2018, 11:33:31 PM »
Phil,

It cycles through each of the individual entities selected, whether stand alone, in solids or in groups, and you have to decide if to flip the points or not. How it assists is, firstly- you don't have to explode solids or groups; secondly, you don't go through clicking on a shared edge not knowing which surface will get selected; third, since Normals mode is only shaded with no transparency or wireframe viewing (where it would be outlined, or even shaded, in the facing color) the macro presents options for being able to view the current surface (so surfaces obscured by other solids or wrapped up in its own solid would be exposed).

But I don't use it too often (I wrote it in Dec 2016, used it a lot then, but haven't used it since) and having just used it I see it needs fixing. Certain things I didn't code in because I could have done them via the View Toolbar. However, now the View Toolbar causes the active window to go back to its default view and that is a problem with this macro.


Lar
« Last Edit: March 30, 2018, 11:36:32 PM by Lar »

Dr PR

  • Hero Member
  • *****
  • Posts: 5588
Re: Macro to export an OBJ file -- Updated!
« Reply #10 on: March 31, 2018, 12:12:27 AM »
Lar,

Thanks. I know I have a LOT of reversed normals in the approximately 2 million objects in the ship model file - they cause some weird shading effects. If I ever want to use the files for 3D printing I will have to clean them up and eliminate reversed normals.

Phil
DesignCAD user since 1987