diff --git a/libsrc/stlgeom/stlgeom.cpp b/libsrc/stlgeom/stlgeom.cpp index 34ef9d8a..b34f38c5 100644 --- a/libsrc/stlgeom/stlgeom.cpp +++ b/libsrc/stlgeom/stlgeom.cpp @@ -3521,6 +3521,75 @@ void STLGeometry :: SmoothGeometry () } } +void STLGeometry :: WriteChartToFile( ChartId chartnumber, string filename ) +{ + PrintMessage(1,"write chart ", int(chartnumber), " to ", filename); + Array trignums; + + if (chartnumber >= 1 && chartnumber <= GetNOCharts()) + { + const STLChart& chart = GetChart(chartnumber); + + for (int j = 1; j <= chart.GetNChartT(); j++) + trignums.Append(chart.GetChartTrig1(j)); + + for (int j = 1; j <= chart.GetNOuterT(); j++) + trignums.Append(chart.GetOuterTrig1(j)); + + QuickSort(trignums); + STLGeometry geo; + NgArray readtrigs; + const auto & first_trig = GetTriangle(chart.GetChartTrig1(1)); + auto normal = first_trig.Normal(); + Box<3> box{Box<3>::EMPTY_BOX}; + + for(auto j : trignums) + { + const auto& trig = GetTriangle(j); + Point<3> pts[3]; + for(auto k : Range(3)) + { + pts[k] = GetPoint(trig[k]); + box.Add(pts[k]); + } + Vec3d normal = Cross( pts[1]-pts[0], pts[2]-pts[0] ); + readtrigs.Append(STLReadTriangle(pts, trig.Normal())); + } + auto dist = box.PMax() - box.PMin(); + auto extra_point = GetPoint(first_trig[0]) - dist.Length()*normal; + + NgArray acttrigs(GetNT()); + acttrigs = -1; + for (int j = 1; j <= chart.GetNT(); j++) + acttrigs.Elem(chart.GetTrig1(j)) = chartnumber; + + for (int j = 1; j <= chart.GetNT(); j++) + { + auto t = chart.GetTrig1(j); + const auto & tt = GetTriangle(t); + for (int k = 1; k <= 3; k++) + { + int nt = NeighbourTrig(t,k); + if (acttrigs.Get(nt) != chartnumber) + { + STLPointId np1, np2; + tt.GetNeighbourPoints(GetTriangle(nt),np1,np2); + + Point<3> pts[3]; + pts[0] = GetPoint(np2); + pts[1] = GetPoint(np1); + pts[2] = extra_point; + Vec3d normal = -Cross( pts[2]-pts[0], pts[1]-pts[0] ); + readtrigs.Append(STLReadTriangle(pts, normal)); + } + } + } + + geo.InitSTLGeometry(readtrigs); + geo.Save(filename); + } +} + class STLGeometryRegister : public GeometryRegister diff --git a/libsrc/stlgeom/stlgeom.hpp b/libsrc/stlgeom/stlgeom.hpp index f7157eb8..76796cd2 100644 --- a/libsrc/stlgeom/stlgeom.hpp +++ b/libsrc/stlgeom/stlgeom.hpp @@ -394,14 +394,14 @@ namespace netgen int TrigIsInOC(int tn, int ocn) const; //get chart number of a trig or 0 if unmarked - ChartId GetChartNr(STLTrigId i) const; + DLL_HEADER ChartId GetChartNr(STLTrigId i) const; ChartId GetMarker(STLTrigId i) const { return chartmark[i]; } void SetMarker(STLTrigId nr, ChartId m); size_t GetNOCharts() const { return atlas.Size(); } //get a chart from atlas const STLChart& GetChart(ChartId nr) const { return *atlas[nr];}; STLChart & GetChart(ChartId nr) { return *atlas[nr];}; - int AtlasMade() const; + DLL_HEADER int AtlasMade() const; void GetInnerChartLimes(NgArray& limes, ChartId chartnum); @@ -460,6 +460,9 @@ namespace netgen int GenerateMesh (shared_ptr & mesh, MeshingParameters & mparam) override; virtual const Refinement & GetRefinement () const override; + + // Add additional Point to chart to close the surface and write the resulting stl to a file + DLL_HEADER void WriteChartToFile( ChartId chartnumber, string filename="chart.slb" ); }; diff --git a/libsrc/stlgeom/stlpkg.cpp b/libsrc/stlgeom/stlpkg.cpp index 0858fa68..3d234bbb 100644 --- a/libsrc/stlgeom/stlpkg.cpp +++ b/libsrc/stlgeom/stlpkg.cpp @@ -395,6 +395,16 @@ namespace netgen { stlgeometry->STLDoctorConfirmedToCandidateEdges(); } + else if (strcmp (argv[1], "writechart") == 0) + { + int st = stlgeometry->GetSelectTrig(); + + if (st >= 1 && st <= stlgeometry->GetNT() && stlgeometry->AtlasMade()) + { + auto chartnumber = stlgeometry->GetChartNr(st); + stlgeometry->WriteChartToFile(chartnumber, "chart.stlb"); + } + } } return TCL_OK; diff --git a/ng/dialog.tcl b/ng/dialog.tcl index e47cbaaa..3a7706dd 100644 --- a/ng/dialog.tcl +++ b/ng/dialog.tcl @@ -944,8 +944,14 @@ proc viewingoptionsdialog { } { -textvariable stloptions.chartnumberoffset -validate focus -takefocus 0 \ -validatecommand "my_validate %W 0 1e9 %P 0" \ -invalidcommand "my_invalid %W" + + ttk::button $f.fn.btn_write_chart -text "Write selected chart to chart.stlb" -command { + Ng_STLDoctor writechart + } + grid $f.fn.lab -sticky ne -padx 4 grid $f.fn.ent -sticky nw -padx 4 -row 1 -column 1 + grid $f.fn.btn_write_chart -padx 4 -row 2 -column 1 grid anchor $f.fn center ttk::labelframe $f.advstl -text "Advanced STL options" -relief groove -borderwidth 3 diff --git a/ng/onetcl.cpp b/ng/onetcl.cpp index 4aabb669..2a9585d5 100644 --- a/ng/onetcl.cpp +++ b/ng/onetcl.cpp @@ -1996,8 +1996,12 @@ const char * ngscript[] = {"" ,"-textvariable stloptions.chartnumberoffset -validate focus -takefocus 0 \\\n" ,"-validatecommand \"my_validate %W 0 1e9 %P 0\" \\\n" ,"-invalidcommand \"my_invalid %W\"\n" +,"ttk::button $f.fn.btn_write_chart -text \"Write selected chart to chart.stlb\" -command {\n" +,"Ng_STLDoctor writechart\n" +,"}\n" ,"grid $f.fn.lab -sticky ne -padx 4\n" ,"grid $f.fn.ent -sticky nw -padx 4 -row 1 -column 1\n" +,"grid $f.fn.btn_write_chart -padx 4 -row 2 -column 1\n" ,"grid anchor $f.fn center\n" ,"ttk::labelframe $f.advstl -text \"Advanced STL options\" -relief groove -borderwidth 3\n" ,"pack $f.advstl -fill x -pady 15\n"