You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
11 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
  5. * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef OCE_VIS_OCE_UTILS_H
  25. #define OCE_VIS_OCE_UTILS_H
  26. #include <list>
  27. #include <map>
  28. #include <string>
  29. #include <utility>
  30. #include <vector>
  31. #include <Standard_Version.hxx>
  32. #include <BRepBuilderAPI_MakeWire.hxx>
  33. #include <TDocStd_Document.hxx>
  34. #include <XCAFApp_Application.hxx>
  35. #include <XCAFDoc_ShapeTool.hxx>
  36. #include <TopoDS_Shape.hxx>
  37. #include <TopoDS_Edge.hxx>
  38. #include <math/vector2d.h>
  39. #include <math/vector3.h>
  40. #include <geometry/shape_poly_set.h>
  41. /**
  42. * Default distance between points to treat them as separate ones (mm)
  43. * 0.001 mm or less is a reasonable value. A too large value creates issues by
  44. * merging points that should be different.
  45. * Remember we are a 3D space, so a thin shape can be broken if 2 points
  46. * are merged (in X, Y, Z coords) when they should not.
  47. * round shapes converted to polygon can also be not good with a to large value
  48. */
  49. static constexpr double OCC_MAX_DISTANCE_TO_MERGE_POINTS = 0.001;
  50. // default PCB thickness in mm
  51. static constexpr double BOARD_THICKNESS_DEFAULT_MM = 1.6;
  52. // minimum PCB thickness in mm (10 microns assumes a very thin polyimide film)
  53. // must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
  54. static constexpr double BOARD_THICKNESS_MIN_MM = 0.01;
  55. // default copper thickness in mm
  56. // must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
  57. static constexpr double COPPER_THICKNESS_DEFAULT_MM = 0.035;
  58. // Max error to approximate an arc by segments (in mm)
  59. static constexpr double ARC_TO_SEGMENT_MAX_ERROR_MM = 0.005;
  60. class PAD;
  61. typedef std::pair< std::string, TDF_Label > MODEL_DATUM;
  62. typedef std::map< std::string, TDF_Label > MODEL_MAP;
  63. extern void ReportMessage( const wxString& aMessage );
  64. class STEP_PCB_MODEL
  65. {
  66. public:
  67. STEP_PCB_MODEL( const wxString& aPcbName );
  68. virtual ~STEP_PCB_MODEL();
  69. // add a pad hole or slot (must be in final position)
  70. bool AddPadHole( const PAD* aPad, const VECTOR2D& aOrigin );
  71. // add a pad shape (must be in final position)
  72. bool AddPadShape( const PAD* aPad, const VECTOR2D& aOrigin );
  73. // add a via shape
  74. bool AddViaShape( const PCB_VIA* aVia, const VECTOR2D& aOrigin );
  75. // add a track segment shape (do not use it for track arcs)
  76. bool AddTrackSegment( const PCB_TRACK* aTrack, const VECTOR2D& aOrigin );
  77. // add a set of polygons (must be in final position) on top or bottom of the board as copper
  78. bool AddCopperPolygonShapes( const SHAPE_POLY_SET* aPolyShapes, bool aOnTop,
  79. const VECTOR2D& aOrigin, bool aTrack );
  80. // add a component at the given position and orientation
  81. bool AddComponent( const std::string& aFileName, const std::string& aRefDes, bool aBottom,
  82. VECTOR2D aPosition, double aRotation, VECTOR3D aOffset,
  83. VECTOR3D aOrientation, VECTOR3D aScale, bool aSubstituteModels = true );
  84. void SetBoardColor( double r, double g, double b );
  85. void SetCopperColor( double r, double g, double b );
  86. // set the thickness of the PCB (mm); the top of the PCB shall be at Z = aThickness
  87. // aThickness < 0.0 == use default thickness
  88. // aThickness <= THICKNESS_MIN == use THICKNESS_MIN
  89. // aThickness > THICKNESS_MIN == use aThickness
  90. void SetPCBThickness( double aThickness );
  91. // Set the max distance (in mm) to consider 2 points have the same coordinates
  92. // and can be merged
  93. void OCCSetMergeMaxDistance( double aDistance = OCC_MAX_DISTANCE_TO_MERGE_POINTS );
  94. void SetMaxError( int aMaxError ) { m_maxError = aMaxError; }
  95. // create the PCB model using the current outlines and drill holes
  96. bool CreatePCB( SHAPE_POLY_SET& aOutline, VECTOR2D aOrigin );
  97. /**
  98. * Convert a SHAPE_POLY_SET to TopoDS_Shape's (polygonal vertical prisms)
  99. * @param aShapes is the TopoDS_Shape list to append to
  100. * @param aPolySet is a polygon set
  101. * @param aThickness is the height of the created prism
  102. * @param aOrigin is the origin of the coordinates
  103. * @return true if success
  104. */
  105. bool MakeShapes( std::vector<TopoDS_Shape>& aShapes, const SHAPE_POLY_SET& aPolySet,
  106. double aThickness, double aZposition, const VECTOR2D& aOrigin );
  107. /**
  108. * Convert a SHAPE_LINE_CHAIN containing only one 360 deg arc to a TopoDS_Shape
  109. * ( vertical cylinder)
  110. * it is a specialized version of MakeShape()
  111. * @param aShape is the TopoDS_Shape to initialize (must be empty)
  112. * @param aChain is a closed SHAPE_LINE_CHAIN, image of a circle: containing one 360 deg arc
  113. * @param aThickness is the height of the created cylinder
  114. * @param aOrigin is the origin of the coordinates
  115. * @return true if success
  116. */
  117. bool MakeShapeAsCylinder( TopoDS_Shape& aShape, const SHAPE_LINE_CHAIN& aChain,
  118. double aThickness, double aZposition, const VECTOR2D& aOrigin );
  119. /**
  120. * Convert a SHAPE_LINE_CHAIN containing only one 360 deg arc to a TopoDS_Shape
  121. * ( vertical cylinder)
  122. * it is a specialized version of MakeShape()
  123. * @param aShape is the TopoDS_Shape to initialize (must be empty)
  124. * @param aStartPoint is the start point of the segment
  125. * @param aEndPoint is the end point of the segment
  126. * @param aWidth is the width of the segment
  127. * @param aThickness is the height of the created cylinder
  128. * @param aOrigin is the origin of the coordinates
  129. * @return true if success
  130. */
  131. bool MakeShapeAsThickSegment( TopoDS_Shape& aShape,
  132. VECTOR2D aStartPoint, VECTOR2D aEndPoint,
  133. double aWidth, double aThickness, double aZposition,
  134. const VECTOR2D& aOrigin );
  135. #ifdef SUPPORTS_IGES
  136. // write the assembly model in IGES format
  137. bool WriteIGES( const wxString& aFileName );
  138. #endif
  139. // write the assembly model in STEP format
  140. bool WriteSTEP( const wxString& aFileName, bool aOptimize );
  141. /**
  142. * Write the assembly in binary GLTF Format
  143. *
  144. * We only support binary GLTF because GLTF is weird
  145. * Officially, binary GLTF is actually json+binary in one file
  146. * If we elected non-binary output with opecascade, it will generate
  147. * that one file as two separate files, one containing json that references the binary
  148. * Which is actually more annoying to deal with (to do the temp file rename, since we dont
  149. * control the binary name) and silly when you can just have the one file.
  150. *
  151. * @param aFileName Output file path
  152. *
  153. * @return true if the write succeeded without error
  154. */
  155. bool WriteGLTF( const wxString& aFileName );
  156. private:
  157. /**
  158. * @return true if the board(s) outline is valid. False otherwise
  159. */
  160. bool isBoardOutlineValid();
  161. /** create one solid board using current outline and drill holes set
  162. * @param aIdx is the main outline index
  163. * @param aOutline is the set of outlines with holes
  164. * @param aOrigin is the coordinate origin for 3 view
  165. */
  166. bool createOneBoard( int aIdx, SHAPE_POLY_SET& aOutline, VECTOR2D aOrigin );
  167. /**
  168. * Load a 3D model data.
  169. *
  170. * @param aFileNameUTF8 is the filename encoded UTF8 (different formats allowed)
  171. * but for WRML files a model data can be loaded instead of the vrml data,
  172. * not suitable in a step file.
  173. * @param aScale is the X,Y,Z scaling factors.
  174. * @param aLabel is the TDF_Label to store the data.
  175. * @param aSubstituteModels = true to allows data substitution, false to disallow.
  176. * @param aErrorMessage (can be nullptr) is an error message to be displayed on error.
  177. * @return true if successfully loaded, false on error.
  178. */
  179. bool getModelLabel( const std::string& aFileNameUTF8, VECTOR3D aScale, TDF_Label& aLabel,
  180. bool aSubstituteModels, wxString* aErrorMessage = nullptr );
  181. bool getModelLocation( bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset,
  182. VECTOR3D aOrientation, TopLoc_Location& aLocation );
  183. bool readIGES( Handle( TDocStd_Document )& m_doc, const char* fname );
  184. bool readSTEP( Handle( TDocStd_Document )& m_doc, const char* fname );
  185. TDF_Label transferModel( Handle( TDocStd_Document )& source, Handle( TDocStd_Document ) & dest,
  186. VECTOR3D aScale );
  187. Handle( XCAFApp_Application ) m_app;
  188. Handle( TDocStd_Document ) m_doc;
  189. Handle( XCAFDoc_ShapeTool ) m_assy;
  190. TDF_Label m_assy_label;
  191. bool m_hasPCB; // set true if CreatePCB() has been invoked
  192. std::vector<TDF_Label> m_pcb_labels; // labels for the PCB model (one by main outline)
  193. MODEL_MAP m_models; // map of file names to model labels
  194. int m_components; // number of successfully loaded components;
  195. double m_precision; // model (length unit) numeric precision
  196. double m_angleprec; // angle numeric precision
  197. double m_boardColor[3]; // board body, RGB values
  198. double m_copperColor[3]; // copper, RGB values
  199. double m_boardThickness; // PCB thickness, mm
  200. double m_copperThickness; // copper thickness, mm
  201. double m_minx; // leftmost curve point
  202. double m_mergeOCCMaxDist; // minimum distance (mm) below which two
  203. // points are considered coincident by OCC
  204. // Holes in main outlines (more than one board)
  205. std::vector<TopoDS_Shape> m_cutouts;
  206. // Main outlines (more than one board)
  207. std::vector<TopoDS_Shape> m_board_outlines;
  208. std::vector<TopoDS_Shape> m_board_copper_zones;
  209. std::vector<TopoDS_Shape> m_board_copper_tracks;
  210. std::vector<TopoDS_Shape> m_board_copper_pads;
  211. /// Name of the PCB, which will most likely be the file name of the path.
  212. wxString m_pcbName;
  213. int m_maxError;
  214. };
  215. #endif // OCE_VIS_OCE_UTILS_H