From 3b3b7fe680684a5e751e39140e561a360a5f76d1 Mon Sep 17 00:00:00 2001 From: Jonathan Westhues Date: Wed, 10 Jun 2009 21:57:23 -0800 Subject: [PATCH] Add STEP file export for 2d curves too, and disable extrusion not normal to the sketch. [git-p4: depot-paths = "//depot/solvespace/": change = 1981] --- export.cpp | 36 +++++++++++++++++++++++++++++++++++- exportstep.cpp | 40 +++++++++++++++++++++++++++++++--------- group.cpp | 6 ++++++ solvespace.cpp | 2 +- solvespace.h | 44 ++++++++++++++++++++++++++++---------------- 5 files changed, 101 insertions(+), 27 deletions(-) diff --git a/export.cpp b/export.cpp index fdd17a5..21d461b 100644 --- a/export.cpp +++ b/export.cpp @@ -319,9 +319,13 @@ VectorFileWriter *VectorFileWriter::ForFile(char *filename) { } else if(StringEndsIn(filename, ".plt")||StringEndsIn(filename, ".hpgl")) { static HpglFileWriter HpglWriter; ret = &HpglWriter; + } else if(StringEndsIn(filename, ".step")||StringEndsIn(filename, ".stp")) { + static Step2dFileWriter Step2dWriter; + ret = &Step2dWriter; } else { Error("Can't identify output file type from file extension of " - "filename '%s'; try .dxf, .svg, .plt, .hpgl, .pdf, .eps, or .ps.", + "filename '%s'; try .step, .stp, .dxf, .svg, .plt, .hpgl, .pdf, " + ".eps, or .ps.", filename); return NULL; } @@ -979,6 +983,36 @@ void HpglFileWriter::FinishAndCloseFile(void) { fclose(f); } +//----------------------------------------------------------------------------- +// Routine for STEP output; just a wrapper around the general STEP stuff that +// can also be used for surfaces or 3d curves. +//----------------------------------------------------------------------------- +void Step2dFileWriter::StartFile(void) { + ZERO(&sfw); + sfw.f = f; + sfw.WriteHeader(); +} + +void Step2dFileWriter::Triangle(STriangle *tr) { +} + +void Step2dFileWriter::LineSegment(double x0, double y0, double x1, double y1) { + SBezier sb = SBezier::From(Vector::From(x0, y0, 0), + Vector::From(x1, y1, 0)); + Bezier(&sb); +} + +void Step2dFileWriter::Bezier(SBezier *sb) { + int c = sfw.ExportCurve(sb); + sfw.curves.Add(&c); +} + +void Step2dFileWriter::FinishAndCloseFile(void) { + sfw.WriteWireframe(); + sfw.WriteFooter(); + fclose(f); +} + //----------------------------------------------------------------------------- // Export the mesh as an STL file; it should always be vertex-to-vertex and // not self-intersecting, so not much to do. diff --git a/exportstep.cpp b/exportstep.cpp index ee216c2..d43c380 100644 --- a/exportstep.cpp +++ b/exportstep.cpp @@ -57,6 +57,9 @@ void StepFileWriter::WriteHeader(void) { "#173=CARTESIAN_POINT('',(0.,0.,0.));\n" "\n" ); + + // Start the ID somewhere beyond the header IDs. + id = 200; } int StepFileWriter::ExportCurve(SBezier *sb) { @@ -320,7 +323,16 @@ void StepFileWriter::ExportSurface(SSurface *ss) { } } -void StepFileWriter::ExportTo(char *file) { +void StepFileWriter::WriteFooter(void) { + fprintf(f, +"\n" +"ENDSEC;\n" +"\n" +"END-ISO-10303-21;\n" + ); +} + +void StepFileWriter::ExportSurfacesTo(char *file) { Group *g = SK.GetGroup(SS.GW.activeGroup); shell = &(g->runningShell); if(shell->surface.n == 0) { @@ -340,8 +352,6 @@ void StepFileWriter::ExportTo(char *file) { WriteHeader(); - id = 200; - ZERO(&advancedFaces); SSurface *ss; @@ -364,14 +374,26 @@ void StepFileWriter::ExportTo(char *file) { fprintf(f, "#%d=SHAPE_REPRESENTATION_RELATIONSHIP($,$,#169,#%d);\n", id+3, id+2); - fprintf(f, -"\n" -"ENDSEC;\n" -"\n" -"END-ISO-10303-21;\n" - ); + WriteFooter(); fclose(f); advancedFaces.Clear(); } +void StepFileWriter::WriteWireframe(void) { + fprintf(f, "#%d=GEOMETRIC_CURVE_SET('curves',(", id); + int *c; + for(c = curves.First(); c; c = curves.NextAfter(c)) { + fprintf(f, "#%d", *c); + if(curves.NextAfter(c) != NULL) fprintf(f, ","); + } + fprintf(f, "));\n"); + fprintf(f, "#%d=GEOMETRICALLY_BOUNDED_WIREFRAME_SHAPE_REPRESENTATION" + "('',(#%d,#170),#168);\n", id+1, id); + fprintf(f, "#%d=SHAPE_REPRESENTATION_RELATIONSHIP($,$,#169,#%d);\n", + id+2, id+1); + + id += 3; + curves.Clear(); +} + diff --git a/group.cpp b/group.cpp index f8705d6..fec6a92 100644 --- a/group.cpp +++ b/group.cpp @@ -78,6 +78,12 @@ void Group::MenuGroup(int id) { break; case GraphicsWindow::MNU_GROUP_EXTRUDE: + if(!SS.GW.LockedInWorkplane()) { + Error("Select a workplane (Sketch -> In Workplane) before " + "extruding. The sketch will be extruded normal to the " + "workplane."); + return; + } g.type = EXTRUDE; g.opA = SS.GW.activeGroup; g.predef.entityB = SS.GW.ActiveWorkplane(); diff --git a/solvespace.cpp b/solvespace.cpp index 1ace0e8..a51242a 100644 --- a/solvespace.cpp +++ b/solvespace.cpp @@ -403,7 +403,7 @@ void SolveSpace::MenuFile(int id) { if(!GetSaveFile(exportFile, SRF_EXT, SRF_PATTERN)) break; StepFileWriter sfw; ZERO(&sfw); - sfw.ExportTo(exportFile); + sfw.ExportSurfacesTo(exportFile); break; } diff --git a/solvespace.h b/solvespace.h index 98c97d8..5aebc68 100644 --- a/solvespace.h +++ b/solvespace.h @@ -84,13 +84,14 @@ int SaveFileYesNoCancel(void); #define SRF_PATTERN "STEP File (*.step;*.stp)\0*.step;*.stp\0" \ "All Files(*)\0*\0\0" #define SRF_EXT "step" -#define VEC_PATTERN "DXF File (*.dxf)\0*.dxf\0" \ +#define VEC_PATTERN "PDF File (*.pdf)\0*.pdf\0" \ "Encapsulated PostScript (*.eps;*.ps)\0*.eps;*.ps\0" \ - "PDF File (*.pdf)\0*.pdf\0" \ "Scalable Vector Graphics (*.svg)\0*.svg\0" \ + "STEP File (*.step;*.stp)\0*.step;*.stp\0" \ + "DXF File (*.dxf)\0*.dxf\0" \ "HPGL File (*.plt;*.hpgl)\0*.plt;*.hpgl\0" \ "All Files (*)\0*\0\0" -#define VEC_EXT "dxf" +#define VEC_EXT "pdf" #define CSV_PATTERN "CSV File (*.csv)\0*.csv\0All Files (*)\0*\0\0" #define CSV_EXT "csv" #define LICENSE_PATTERN \ @@ -361,6 +362,23 @@ public: SBezierList *sbl, Vector origin, Vector u, Vector v); }; +class StepFileWriter { +public: + void ExportSurfacesTo(char *filename); + void WriteHeader(void); + int ExportCurve(SBezier *sb); + int ExportCurveLoop(SBezierLoop *loop, bool inner); + void ExportSurface(SSurface *ss); + void WriteWireframe(void); + void WriteFooter(void); + + List curves; + List advancedFaces; + SShell *shell; + FILE *f; + int id; +}; + class VectorFileWriter { public: FILE *f; @@ -426,19 +444,13 @@ public: void StartFile(void); void FinishAndCloseFile(void); }; - -class StepFileWriter { -public: - void ExportTo(char *filename); - void WriteHeader(void); - int ExportCurve(SBezier *sb); - int ExportCurveLoop(SBezierLoop *loop, bool inner); - void ExportSurface(SSurface *ss); - - List advancedFaces; - SShell *shell; - FILE *f; - int id; +class Step2dFileWriter : public VectorFileWriter { + StepFileWriter sfw; + void LineSegment(double x0, double y0, double x1, double y1); + void Triangle(STriangle *tr); + void Bezier(SBezier *sb); + void StartFile(void); + void FinishAndCloseFile(void); }; #ifdef LIBRARY