Author Topic: Flatten STEP to SVG script  (Read 5504 times)

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Flatten STEP to SVG script
« on: September 06, 2024, 14:38:48 pm »
Hi All!

I have started a script to flatten STEP files into a 2D SVG file. So far it works, with caveats, on two issues.  The script has additional info on the intended purpose.  Attachment has been updated to current version.

1.
Currently it just uses a pre-defined face for the processing, but does output the SVG correctly.

I could use some assistance with determining the appropriate target face.

  I'm thinking I could:

      Iterate over all of the Surface features in the  first (imported) layer
         Find the faces with the largest area (by extents?)
         (2 solutions should be found) both on XY plane, which are the Front and Back
         Of the two, choose surface with highest Z (Front)
         Get the ID for the surface face (TARGET_FACE_INDEX)
         Populate ID in getface()

   Can anyone provide code suggestions or a better method?
 
2.
  The script fails on the first run after CB is opened,   All runs thereafter work
  I suspect it has something to do with the initial file load and view.CurrentEditMode always being 'None'


Attached is the script and a test STEP file.  Paths are hardcoded to C:\TEMP right now to test.

If any code looks odd it's because I'm just taking a stab at this.  This is my second Python script...

Any help is greatly appreciated!


I refactored the code, and added automatic face detection.  It now it working with one issue


   Open Issue!
#   Issue with SVG export of certain Polylines
#   consisting of an single Arc and a singe Line
#   whether manually saved from the toolbar
#   or executed from script. Suspecting
#   SVG Export plugin at this point
#
#       Repeatability:
#           Manually import REAR-HIP from zip
#           Select surface 52 in layer tree
#           Edit->Surface->Silhouette (Zero value)
#           Delete everything but the 6 added Polylines
#           File->Export->SVG
#           Load created SVG in (Inkscape, Browser, Deepnest)
#           (Deepnest needs the XML header line removed to view)
#       RESULT:  Circles are missing (Polylines 55 and 59)
#           Exploding PLs 55,59 "circles" shows that they consist
#           of an Arc and Line.
#       Continuing to investigate...

EddyCurrrent if you see this post can you take a look please?

Thanks,
Paul

« Last Edit: September 11, 2024, 04:09:46 am by nm156 »
machines have no conscience

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Flatten STEP to SVG script
« Reply #1 on: September 08, 2024, 01:16:25 am »

Initial post edited, updated with current script version.

Paul
machines have no conscience

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Flatten STEP to SVG script
« Reply #2 on: September 08, 2024, 18:22:24 pm »
OK, further investigation...

When CB loads a STEP file, it correctly parses circles as such. Silhouette, however, creates an odd combination of an arc and line as circles.

If I draw a native arc in CB and use SVG Export it works correcly.  However, it appears that CB will only allow an arc to be created with a maximum sweep of 180 degrees. (Interactively from the GUI)

The surface->silhouette creates arc sweeps that can be much larger, I've seen up to around 357+ degrees. 

Since CB correctly displays the arc it created with silhouette, it can handle larger sweeps. I am presuming that the plugin expects arc sweeps to be less than or equal to 180 degrees when exporting. (As that is the maximum that can be drawn interactively)

I think my next step :) will be to examine the Silhouette created polylines and determine if they are path closed and consist of an arc and line within a tolerance and call it a circle.  Or possibly identify the true circles in the STEP import, and compare to the errored polylines to fix the arc/line issue.

Then further preprocess into a primitive circle in the script, prior to SVG output.

Still plugging away in my spare time...

Paul


« Last Edit: September 08, 2024, 22:57:04 pm by nm156 »
machines have no conscience

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Flatten STEP to SVG script
« Reply #3 on: September 09, 2024, 14:05:23 pm »
<PROJECT COMPLETE>

Updated the first post with completed project.  All of the STEP files I tried have processed correctly.  There are some additional demo STEP files in the zip archive .  I'm calling this one done.

Issues that were (probably) overcome:

There is some strangeness with both the STEP import and the Silhouette functions in CamBam 1.0

STEP import creates Circles with a Center of 0,0,0 even though they are displayed correctly.  I also attempted to make a copy of said Circles, but the copy also has a  a zero center.  (I'm guessing a deep copy somewhere in CB is incorrect)  This means that I can't use them as a compare reference to fix the below issue.

Silhouette creates the an Arc, Line combination as a Polyline to represent a Circle (even when the source is a true circle).  In the script I attempt to create a new Circle based on the Sweep and the criteria  that it consists of exactly one Polyline, Is Closed, and is comprised of 1 Arc and 1 Line.  This may be an issue with "D" shaped objects, so warning is issued when the conversion is performed. I can't think of a better way at the moment.  The Sweep threshold is a variable by default set to 340 degrees.

So use with caution, and verify the output SVG, but so far I'm pleased with the results.

Paul

machines have no conscience

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7422
    • View Profile
    • Cambam V1.0 French Doc
Re: Flatten STEP to SVG script
« Reply #4 on: September 09, 2024, 14:38:16 pm »
Hello

Quote
STEP import creates Circles with a Center of 0,0,0 even though they are displayed correctly.

Yes because they use a transformation matrix, but if you "apply transformation" the center and the diameter are updated with the right values.
(edit/transform/apply transformations)

This script apply transformation and set the object to "identity" for all selected objects ; all 2D objects and 3D surfaces use a transformation matrix after a STEP file has been loaded

Code: [Select]
# New CamBam Python Script
for ent in view.Selection:
    ent.ApplyTransformation()
    ent.Transform = Matrix4x4F.Identity

++
David
« Last Edit: September 09, 2024, 19:30:14 pm by dh42 »

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7422
    • View Profile
    • Cambam V1.0 French Doc
Re: Flatten STEP to SVG script
« Reply #5 on: September 10, 2024, 13:42:33 pm »
I just reactivated your account (needed because you changed you mail address)

++
David

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Flatten STEP to SVG script
« Reply #6 on: September 10, 2024, 17:33:28 pm »

David,

Thank you much appreciated!

And many thanks for helping me resolve the issue with the circle transformation!

I have a an updated script with the changes, that I'll be posting soon,


Paul
machines have no conscience

Offline Dragonfly

  • CNC Jedi
  • *****
  • Posts: 2656
    • View Profile
Re: Flatten STEP to SVG script
« Reply #7 on: September 10, 2024, 18:38:30 pm »
Code: [Select]
    ent.ApplyTransformation()
    ent.Transform = Matrix4x4F.Identity
I used to think that the second operation is done automatically by CB when transformation is applied.
And shouldn't there be a boolean check of the ApplyTransformation method?

Offline nm156

  • Ewok
  • *
  • Posts: 23
    • View Profile
Re: Flatten STEP to SVG script
« Reply #8 on: September 11, 2024, 04:18:38 am »
machines have no conscience

Offline dh42

  • Administrator
  • CNC Jedi
  • *****
  • Posts: 7422
    • View Profile
    • Cambam V1.0 French Doc
Re: Flatten STEP to SVG script
« Reply #9 on: September 11, 2024, 16:08:37 pm »
Hello

Quote
I used to think that the second operation is done automatically by CB when transformation is applied.

me too, but if I use only ApplyTransformation, the values are updated in center and diameter but the matrix is not set to identity.

Quote
And shouldn't there be a boolean check of the ApplyTransformation method?

maybe, but I'm really not a python coder, took me 30min just to get this 3 lines working LOL ...

++
David