KiCad PCB EDA Suite
Loading...
Searching...
No Matches
step_pcb_model.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 Cirilo Bernardo <[email protected]>
5 * Copyright The 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, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef OCE_VIS_OCE_UTILS_H
22#define OCE_VIS_OCE_UTILS_H
23
24#include <list>
25#include <map>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include <Standard_Handle.hxx>
31#include <TDF_Label.hxx>
32#include <TopoDS_Shape.hxx>
33
34#include <math/vector2d.h>
35#include <math/vector3.h>
38
47static constexpr double OCC_MAX_DISTANCE_TO_MERGE_POINTS = 0.001;
48
49// default PCB thickness in mm
50static constexpr double BOARD_THICKNESS_DEFAULT_MM = 1.6;
51
52// minimum PCB thickness in mm (10 microns assumes a very thin polyimide film)
53// must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
54static constexpr double BOARD_THICKNESS_MIN_MM = 0.01;
55
56// default copper thickness in mm
57// must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
58static constexpr double COPPER_THICKNESS_DEFAULT_MM = 0.035;
59
60// Max error to approximate an arc by segments (in mm)
61static constexpr double ARC_TO_SEGMENT_MAX_ERROR_MM = 0.005;
62
63class FOOTPRINT;
64class PAD;
65
66class TDocStd_Document;
67class XCAFApp_Application;
68class XCAFDoc_ColorTool;
69class XCAFDoc_ShapeTool;
70
71typedef std::pair< std::string, TDF_Label > MODEL_DATUM;
72typedef std::map< std::string, TDF_Label > MODEL_MAP;
73
74
89
91{
92public:
93 STEP_PCB_MODEL( const wxString& aPcbName, REPORTER* aReporter );
94 virtual ~STEP_PCB_MODEL();
95
96 // Update m_outFmt to aVariant, giving the output format variant
97 void SpecializeVariant( OUTPUT_FORMAT aVariant ) { m_outFmt = aVariant; }
98
99 // add a pad shape (must be in final position)
100 // if aClipPolygon is not nullptr, the pad shape will be clipped by aClipPolygon
101 // (usually aClipPolygon is the board outlines and use for castelleted pads)
102 bool AddPadShape( const PAD* aPad, const VECTOR2D& aOrigin, bool aVia,
103 SHAPE_POLY_SET* aClipPolygon = nullptr );
104
105 // add a pad hole or slot (must be in final position)
106 bool AddHole( const SHAPE_SEGMENT& aShape, int aPlatingThickness, PCB_LAYER_ID aLayerTop,
107 PCB_LAYER_ID aLayerBot, bool aVia, const VECTOR2D& aOrigin, bool aCutCopper,
108 bool aCutBody );
109
110 // add a plated hole shape (without the hole)
111 bool AddBarrel( const SHAPE_SEGMENT& aShape, PCB_LAYER_ID aLayerTop, PCB_LAYER_ID aLayerBot,
112 bool aVia, const VECTOR2D& aOrigin, const wxString& aNetname );
113
127 bool AddBackdrill( const SHAPE_SEGMENT& aShape, PCB_LAYER_ID aLayerStart,
128 PCB_LAYER_ID aLayerEnd, const VECTOR2D& aOrigin );
129
143 bool AddCounterbore( const VECTOR2I& aPosition, int aDiameter, int aDepth,
144 bool aFrontSide, const VECTOR2D& aOrigin );
145
161 bool AddCountersink( const VECTOR2I& aPosition, int aDiameter, int aDepth, int aAngle,
162 bool aFrontSide, const VECTOR2D& aOrigin );
163
177 std::map<PCB_LAYER_ID, int> GetCopperLayerKnockouts( int aDiameter, int aDepth,
178 int aAngle, bool aFrontSide );
179
180 // add a set of polygons (must be in final position)
181 bool AddPolygonShapes( const SHAPE_POLY_SET* aPolyShapes, PCB_LAYER_ID aLayer,
182 const VECTOR2D& aOrigin, const wxString& aNetname );
183
184 // add a component at the given position and orientation
185 bool AddComponent( const wxString& aBaseName, const wxString& aFileName,
186 const std::vector<wxString>& aAltFilenames, const wxString& aRefDes,
187 bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset,
188 VECTOR3D aOrientation, VECTOR3D aScale, bool aSubstituteModels = true );
189
190 void SetCopperColor( double r, double g, double b );
191 void SetPadColor( double r, double g, double b );
192
193 void SetEnabledLayers( const LSET& aLayers );
194 void SetFuseShapes( bool aValue );
195 void SetSimplifyShapes( bool aValue );
196 void SetStackup( const BOARD_STACKUP& aStackup );
197 void SetNetFilter( const wxString& aFilter );
198 void SetExtraPadThickness( bool aValue );
199
200 // Set the max distance (in mm) to consider 2 points have the same coordinates
201 // and can be merged
203
204 // create the PCB model using the current outlines and drill holes
205 bool CreatePCB( SHAPE_POLY_SET& aOutline, const VECTOR2D& aOrigin, bool aPushBoardBody );
206
216 bool MakeShapes( std::vector<TopoDS_Shape>& aShapes, const SHAPE_POLY_SET& aPolySet,
217 bool aConvertToArcs, double aThickness, double aZposition, const VECTOR2D& aOrigin );
218
231 bool AddExtrudedBody( const SHAPE_POLY_SET& aOutline, bool aBottom, double aStandoff, double aHeight,
232 const VECTOR2D& aOrigin, uint32_t aColor, EXTRUSION_MATERIAL aMaterial,
233 const wxString& aRefDes );
234
244 bool AddExtrudedPins( const FOOTPRINT* aFootprint, bool aBottom, double aStandoff, const VECTOR2D& aOrigin );
245
257 bool MakeShapeAsThickSegment( TopoDS_Shape& aShape, const VECTOR2D& aStartPoint,
258 const VECTOR2D& aEndPoint, double aWidth, double aThickness,
259 double aZposition, const VECTOR2D& aOrigin );
260
271 bool MakePolygonAsWall( TopoDS_Shape& aShape, SHAPE_POLY_SET& aPolySet, double aHeight,
272 double aZposition, const VECTOR2D& aOrigin );
273
274#ifdef SUPPORTS_IGES
275 // write the assembly model in IGES format
276 bool WriteIGES( const wxString& aFileName );
277#endif
278
279 // write the assembly model in STEP format
280 bool WriteSTEP( const wxString& aFileName, bool aOptimize, bool compress );
281
282 // write the assembly in BREP format
283 bool WriteBREP( const wxString& aFileName );
284
285 // write the assembly in XAO format with pad faces as groups
286 bool WriteXAO( const wxString& aFileName );
287
302 bool WriteGLTF( const wxString& aFileName );
303
304 // write the assembly in PLY format (mesh)
305 bool WritePLY( const wxString& aFileName );
306
307 // write the assembly in STL format (mesh)
308 bool WriteSTL( const wxString& aFileName );
309
310 // write the assembly in U3D format (mesh)
311 bool WriteU3D( const wxString& aFileName );
312
313 // write the assembly in U3D format (mesh)
314 bool WritePDF( const wxString& aFileName );
315private:
319 bool isBoardOutlineValid();
320
321 void getLayerZPlacement( PCB_LAYER_ID aLayer, double& aZPos, double& aThickness );
322
323 void getCopperLayerZPlacement( PCB_LAYER_ID aLayer, double& aZPos, double& aThickness );
324
325 void getBoardBodyZPlacement( double& aZPos, double& aThickness );
326
341 bool getModelLabel( const wxString& aBaseName, const wxString& aFileName,
342 const std::vector<wxString>& aAltFilenames, VECTOR3D aScale,
343 TDF_Label& aLabel, bool aSubstituteModels,
344 wxString* aErrorMessage = nullptr );
345
346 bool getModelLocation( bool aBottom, const VECTOR2D& aPosition, double aRotation, const VECTOR3D& aOffset,
347 const VECTOR3D& aOrientation, TopLoc_Location& aLocation );
348
349 bool readIGES( Handle( TDocStd_Document ) & aDoc, const char* aFname );
350 bool readSTEP( Handle( TDocStd_Document ) & aDoc, const char* aFname );
351 bool readVRML( Handle( TDocStd_Document ) & aDoc, const char* aFname );
352
353 bool performMeshing( Handle( XCAFDoc_ShapeTool ) & aShapeTool );
354
355 TDF_Label transferModel( Handle( TDocStd_Document )& source, Handle( TDocStd_Document ) & dest,
356 const VECTOR3D& aScale );
357
358 bool CompressSTEP( wxString& inputFile, wxString& outputFile );
359
360 Handle( XCAFApp_Application ) m_app;
361 Handle( TDocStd_Document ) m_doc;
362 Handle( XCAFDoc_ShapeTool ) m_assy;
363 TDF_Label m_assy_label;
364 bool m_hasPCB; // set true if CreatePCB() has been invoked
365 bool m_simplifyShapes; // convert parts of outlines to arcs where possible
366 bool m_fuseShapes; // fuse geometry together
367 bool m_extraPadThickness; // add extra thickness to pads
368 std::vector<TDF_Label> m_pcb_labels; // labels for the PCB model (one by main outline)
369 MODEL_MAP m_models; // map of file names to model labels
370 int m_components; // number of successfully loaded components;
371 double m_precision; // model (length unit) numeric precision
372 double m_angleprec; // angle numeric precision
373 double m_copperColor[3]; // copper, RGB values
374 double m_padColor[3]; // pads, RGB values
375 BOARD_STACKUP m_stackup; // board stackup
376 LSET m_enabledLayers; // a set of layers enabled for export
377 wxString m_netFilter; // remove nets not matching this wildcard
378
379 double m_minx; // leftmost curve point
380 double m_mergeOCCMaxDist; // minimum distance (mm) below which two
381 // points are considered coincident by OCC
382
383 // Holes in copper and main outlines
384 std::vector<TopoDS_Shape> m_copperCutouts;
385 std::vector<TopoDS_Shape> m_boardCutouts;
386
387 // Main outlines (more than one board)
388 std::vector<TopoDS_Shape> m_board_outlines;
389
390 // Copper items. Key is netname.
391 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper;
392 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_pads;
393 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_vias;
394 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_fused;
395
396 // Graphical items
397 std::vector<TopoDS_Shape> m_board_front_silk;
398 std::vector<TopoDS_Shape> m_board_back_silk;
399 std::vector<TopoDS_Shape> m_board_front_mask;
400 std::vector<TopoDS_Shape> m_board_back_mask;
401
402 // Extruded 3D bodies from footprint outlines
404 {
405 std::vector<TopoDS_Shape> bodyShapes;
406 std::vector<TopoDS_Shape> pinShapes;
407 wxString refDes;
408 uint32_t colorKey;
410 };
411
412 std::vector<EXTRUDED_BODY_ENTRY> m_extruded_bodies;
413
414 // Data for pads. Key example: Pad_F_U2_1_GND
415 std::map<wxString, std::vector<std::pair<gp_Pnt, TopoDS_Shape>>> m_pad_points;
416
418 wxString m_pcbName;
419
423};
424
425#endif // OCE_VIS_OCE_UTILS_H
Manage layers needed to make a physical board.
LSET is a set of PCB_LAYER_IDs.
Definition lset.h:37
Definition pad.h:61
A pure virtual class used to derive REPORTER objects from.
Definition reporter.h:71
Represent a set of closed polygons.
bool MakeShapeAsThickSegment(TopoDS_Shape &aShape, const VECTOR2D &aStartPoint, const VECTOR2D &aEndPoint, double aWidth, double aThickness, double aZposition, const VECTOR2D &aOrigin)
Make a segment shape based on start and end point.
OUTPUT_FORMAT m_outFmt
The current output format for created file.
void SetCopperColor(double r, double g, double b)
bool WritePLY(const wxString &aFileName)
bool AddCountersink(const VECTOR2I &aPosition, int aDiameter, int aDepth, int aAngle, bool aFrontSide, const VECTOR2D &aOrigin)
Add a countersink shape to remove board material from the top or bottom of a hole.
std::map< wxString, std::vector< TopoDS_Shape > > m_board_copper_vias
std::map< wxString, std::vector< TopoDS_Shape > > m_board_copper_pads
bool WriteSTEP(const wxString &aFileName, bool aOptimize, bool compress)
std::vector< TopoDS_Shape > m_board_back_mask
wxString m_pcbName
Name of the PCB, which will most likely be the file name of the path.
std::vector< TopoDS_Shape > m_boardCutouts
bool AddPolygonShapes(const SHAPE_POLY_SET *aPolyShapes, PCB_LAYER_ID aLayer, const VECTOR2D &aOrigin, const wxString &aNetname)
bool CreatePCB(SHAPE_POLY_SET &aOutline, const VECTOR2D &aOrigin, bool aPushBoardBody)
void getBoardBodyZPlacement(double &aZPos, double &aThickness)
TDF_Label transferModel(Handle(TDocStd_Document)&source, Handle(TDocStd_Document) &dest, const VECTOR3D &aScale)
TDF_Label m_assy_label
bool getModelLocation(bool aBottom, const VECTOR2D &aPosition, double aRotation, const VECTOR3D &aOffset, const VECTOR3D &aOrientation, TopLoc_Location &aLocation)
void SetFuseShapes(bool aValue)
bool WriteXAO(const wxString &aFileName)
bool WriteGLTF(const wxString &aFileName)
Write the assembly in binary GLTF Format.
Handle(XCAFDoc_ShapeTool) m_assy
REPORTER * m_reporter
std::vector< TDF_Label > m_pcb_labels
STEP_PCB_MODEL(const wxString &aPcbName, REPORTER *aReporter)
std::vector< EXTRUDED_BODY_ENTRY > m_extruded_bodies
bool AddBackdrill(const SHAPE_SEGMENT &aShape, PCB_LAYER_ID aLayerStart, PCB_LAYER_ID aLayerEnd, const VECTOR2D &aOrigin)
Add a backdrill hole shape to remove board material and copper plating.
void getLayerZPlacement(PCB_LAYER_ID aLayer, double &aZPos, double &aThickness)
std::vector< TopoDS_Shape > m_board_outlines
void SetSimplifyShapes(bool aValue)
bool WriteU3D(const wxString &aFileName)
bool getModelLabel(const wxString &aBaseName, const wxString &aFileName, const std::vector< wxString > &aAltFilenames, VECTOR3D aScale, TDF_Label &aLabel, bool aSubstituteModels, wxString *aErrorMessage=nullptr)
Load a 3D model data.
bool readVRML(Handle(TDocStd_Document) &aDoc, const char *aFname)
void SetPadColor(double r, double g, double b)
bool AddCounterbore(const VECTOR2I &aPosition, int aDiameter, int aDepth, bool aFrontSide, const VECTOR2D &aOrigin)
Add a counterbore shape to remove board material from the top or bottom of a hole.
void SetEnabledLayers(const LSET &aLayers)
void SetExtraPadThickness(bool aValue)
bool performMeshing(Handle(XCAFDoc_ShapeTool) &aShapeTool)
bool MakePolygonAsWall(TopoDS_Shape &aShape, SHAPE_POLY_SET &aPolySet, double aHeight, double aZposition, const VECTOR2D &aOrigin)
Make a polygonal shape to create a vertical wall.
bool readSTEP(Handle(TDocStd_Document) &aDoc, const char *aFname)
Handle(TDocStd_Document) m_doc
std::vector< TopoDS_Shape > m_board_front_silk
bool AddExtrudedPins(const FOOTPRINT *aFootprint, bool aBottom, double aStandoff, const VECTOR2D &aOrigin)
Add metallic pin extrusions for through-hole pads.
Handle(XCAFApp_Application) m_app
bool WritePDF(const wxString &aFileName)
virtual ~STEP_PCB_MODEL()
std::vector< TopoDS_Shape > m_board_front_mask
double m_copperColor[3]
bool WriteSTL(const wxString &aFileName)
void SetStackup(const BOARD_STACKUP &aStackup)
void SetNetFilter(const wxString &aFilter)
double m_padColor[3]
std::map< wxString, std::vector< TopoDS_Shape > > m_board_copper
std::map< PCB_LAYER_ID, int > GetCopperLayerKnockouts(int aDiameter, int aDepth, int aAngle, bool aFrontSide)
Get the knockout diameters for copper layers that a counterbore or countersink crosses.
bool MakeShapes(std::vector< TopoDS_Shape > &aShapes, const SHAPE_POLY_SET &aPolySet, bool aConvertToArcs, double aThickness, double aZposition, const VECTOR2D &aOrigin)
Convert a SHAPE_POLY_SET to TopoDS_Shape's (polygonal vertical prisms, or flat faces)
bool AddBarrel(const SHAPE_SEGMENT &aShape, PCB_LAYER_ID aLayerTop, PCB_LAYER_ID aLayerBot, bool aVia, const VECTOR2D &aOrigin, const wxString &aNetname)
bool readIGES(Handle(TDocStd_Document) &aDoc, const char *aFname)
bool AddHole(const SHAPE_SEGMENT &aShape, int aPlatingThickness, PCB_LAYER_ID aLayerTop, PCB_LAYER_ID aLayerBot, bool aVia, const VECTOR2D &aOrigin, bool aCutCopper, bool aCutBody)
std::vector< TopoDS_Shape > m_copperCutouts
void SpecializeVariant(OUTPUT_FORMAT aVariant)
bool CompressSTEP(wxString &inputFile, wxString &outputFile)
std::vector< TopoDS_Shape > m_board_back_silk
void getCopperLayerZPlacement(PCB_LAYER_ID aLayer, double &aZPos, double &aThickness)
bool AddComponent(const wxString &aBaseName, const wxString &aFileName, const std::vector< wxString > &aAltFilenames, const wxString &aRefDes, bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset, VECTOR3D aOrientation, VECTOR3D aScale, bool aSubstituteModels=true)
bool AddExtrudedBody(const SHAPE_POLY_SET &aOutline, bool aBottom, double aStandoff, double aHeight, const VECTOR2D &aOrigin, uint32_t aColor, EXTRUSION_MATERIAL aMaterial, const wxString &aRefDes)
Add an extruded 3D body from a 2D outline polygon.
std::map< wxString, std::vector< std::pair< gp_Pnt, TopoDS_Shape > > > m_pad_points
bool AddPadShape(const PAD *aPad, const VECTOR2D &aOrigin, bool aVia, SHAPE_POLY_SET *aClipPolygon=nullptr)
std::map< wxString, std::vector< TopoDS_Shape > > m_board_copper_fused
void OCCSetMergeMaxDistance(double aDistance=OCC_MAX_DISTANCE_TO_MERGE_POINTS)
BOARD_STACKUP m_stackup
bool WriteBREP(const wxString &aFileName)
EXTRUSION_MATERIAL
Definition footprint.h:93
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
static constexpr double COPPER_THICKNESS_DEFAULT_MM
OUTPUT_FORMAT
static constexpr double OCC_MAX_DISTANCE_TO_MERGE_POINTS
Default distance between points to treat them as separate ones (mm) 0.001 mm or less is a reasonable ...
std::map< std::string, TDF_Label > MODEL_MAP
static constexpr double BOARD_THICKNESS_DEFAULT_MM
std::pair< std::string, TDF_Label > MODEL_DATUM
static constexpr double ARC_TO_SEGMENT_MAX_ERROR_MM
static constexpr double BOARD_THICKNESS_MIN_MM
EXTRUSION_MATERIAL material
std::vector< TopoDS_Shape > bodyShapes
uint32_t colorKey
std::vector< TopoDS_Shape > pinShapes
wxString refDes
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
VECTOR3< double > VECTOR3D
Definition vector3.h:230