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#include <reporter.h>
39
48static constexpr double OCC_MAX_DISTANCE_TO_MERGE_POINTS = 0.001;
49
50// default PCB thickness in mm
51static constexpr double BOARD_THICKNESS_DEFAULT_MM = 1.6;
52
53// minimum PCB thickness in mm (10 microns assumes a very thin polyimide film)
54// must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
55static constexpr double BOARD_THICKNESS_MIN_MM = 0.01;
56
57// default copper thickness in mm
58// must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
59static constexpr double COPPER_THICKNESS_DEFAULT_MM = 0.035;
60
61// Max error to approximate an arc by segments (in mm)
62static constexpr double ARC_TO_SEGMENT_MAX_ERROR_MM = 0.005;
63
64class FOOTPRINT;
65class PAD;
66enum class EXTRUSION_MATERIAL;
68
69class TDocStd_Document;
70class XCAFApp_Application;
71class XCAFDoc_ColorTool;
72class XCAFDoc_ShapeTool;
73
74typedef std::pair< std::string, TDF_Label > MODEL_DATUM;
75typedef std::map< std::string, TDF_Label > MODEL_MAP;
76
77
92
94{
95public:
96 STEP_PCB_MODEL( const wxString& aPcbName, REPORTER* aReporter );
97 virtual ~STEP_PCB_MODEL();
98
99 // Update m_outFmt to aVariant, giving the output format variant
100 void SpecializeVariant( OUTPUT_FORMAT aVariant ) { m_outFmt = aVariant; }
101
102 // add a pad shape (must be in final position)
103 // if aClipPolygon is not nullptr, the pad shape will be clipped by aClipPolygon
104 // (usually aClipPolygon is the board outlines and use for castelleted pads)
105 bool AddPadShape( const PAD* aPad, const VECTOR2D& aOrigin, bool aVia,
106 SHAPE_POLY_SET* aClipPolygon = nullptr );
107
108 // add a pad hole or slot (must be in final position)
109 bool AddHole( const SHAPE_SEGMENT& aShape, int aPlatingThickness, PCB_LAYER_ID aLayerTop,
110 PCB_LAYER_ID aLayerBot, bool aVia, const VECTOR2D& aOrigin, bool aCutCopper,
111 bool aCutBody );
112
113 // add a plated hole shape (without the hole)
114 bool AddBarrel( const SHAPE_SEGMENT& aShape, PCB_LAYER_ID aLayerTop, PCB_LAYER_ID aLayerBot,
115 bool aVia, const VECTOR2D& aOrigin, const wxString& aNetname );
116
130 bool AddBackdrill( const SHAPE_SEGMENT& aShape, PCB_LAYER_ID aLayerStart,
131 PCB_LAYER_ID aLayerEnd, const VECTOR2D& aOrigin );
132
146 bool AddCounterbore( const VECTOR2I& aPosition, int aDiameter, int aDepth,
147 bool aFrontSide, const VECTOR2D& aOrigin );
148
164 bool AddCountersink( const VECTOR2I& aPosition, int aDiameter, int aDepth, int aAngle,
165 bool aFrontSide, const VECTOR2D& aOrigin );
166
180 std::map<PCB_LAYER_ID, int> GetCopperLayerKnockouts( int aDiameter, int aDepth,
181 int aAngle, bool aFrontSide );
182
183 // add a set of polygons (must be in final position)
184 bool AddPolygonShapes( const SHAPE_POLY_SET* aPolyShapes, PCB_LAYER_ID aLayer,
185 const VECTOR2D& aOrigin, const wxString& aNetname );
186
187 // add a component at the given position and orientation
188 bool AddComponent( const wxString& aBaseName, const wxString& aFileName,
189 const std::vector<wxString>& aAltFilenames, const wxString& aRefDes,
190 bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset,
191 VECTOR3D aOrientation, VECTOR3D aScale, bool aSubstituteModels = true );
192
193 void SetCopperColor( double r, double g, double b );
194 void SetPadColor( double r, double g, double b );
195
196 void SetEnabledLayers( const LSET& aLayers );
197 void SetFuseShapes( bool aValue );
198 void SetSimplifyShapes( bool aValue );
199 void SetStackup( const BOARD_STACKUP& aStackup );
200 void SetNetFilter( const wxString& aFilter );
201 void SetExtraPadThickness( bool aValue );
202
203 // Set the max distance (in mm) to consider 2 points have the same coordinates
204 // and can be merged
206
207 // create the PCB model using the current outlines and drill holes
208 bool CreatePCB( SHAPE_POLY_SET& aOutline, const VECTOR2D& aOrigin, bool aPushBoardBody );
209
221 bool MakeShapes( std::vector<TopoDS_Shape>& aShapes, const SHAPE_POLY_SET& aPolySet,
222 bool aConvertToArcs, double aThickness, double aZposition, const VECTOR2D& aOrigin );
223
236 bool AddExtrudedBody( const SHAPE_POLY_SET& aOutline, bool aBottom, double aStandoff, double aHeight,
237 const VECTOR2D& aOrigin, uint32_t aColor, EXTRUSION_MATERIAL aMaterial,
238 const wxString& aRefDes );
239
250 bool AddExtrudedPins( const FOOTPRINT* aFootprint, const EXTRUDED_3D_BODY* aBody, bool aBottom, double aStandoff,
251 const VECTOR2D& aOrigin );
252
265 bool MakeShapeAsThickSegment( TopoDS_Shape& aShape, const VECTOR2D& aStartPoint,
266 const VECTOR2D& aEndPoint, double aWidth, double aThickness,
267 double aZposition, const VECTOR2D& aOrigin );
268
279 bool MakePolygonAsWall( TopoDS_Shape& aShape, SHAPE_POLY_SET& aPolySet, double aHeight,
280 double aZposition, const VECTOR2D& aOrigin );
281
282#ifdef SUPPORTS_IGES
283 // write the assembly model in IGES format
284 bool WriteIGES( const wxString& aFileName );
285#endif
286
287 // write the assembly model in STEP format
288 bool WriteSTEP( const wxString& aFileName, bool aOptimize, bool compress );
289
290 // write the assembly in BREP format
291 bool WriteBREP( const wxString& aFileName );
292
293 // write the assembly in XAO format with pad faces as groups
294 bool WriteXAO( const wxString& aFileName );
295
310 bool WriteGLTF( const wxString& aFileName );
311
312 // write the assembly in PLY format (mesh)
313 bool WritePLY( const wxString& aFileName );
314
315 // write the assembly in STL format (mesh)
316 bool WriteSTL( const wxString& aFileName );
317
318 // write the assembly in U3D format (mesh)
319 bool WriteU3D( const wxString& aFileName );
320
321 // write the assembly in U3D format (mesh)
322 bool WritePDF( const wxString& aFileName );
323private:
327 bool isBoardOutlineValid();
328
329 void getLayerZPlacement( PCB_LAYER_ID aLayer, double& aZPos, double& aThickness );
330
331 void getCopperLayerZPlacement( PCB_LAYER_ID aLayer, double& aZPos, double& aThickness );
332
333 void getBoardBodyZPlacement( double& aZPos, double& aThickness );
334
339 double getStackupBodyThickness() const;
340
355 bool getModelLabel( const wxString& aBaseName, const wxString& aFileName,
356 const std::vector<wxString>& aAltFilenames, VECTOR3D aScale,
357 TDF_Label& aLabel, bool aSubstituteModels,
358 wxString* aErrorMessage = nullptr );
359
360 bool getModelLocation( bool aBottom, const VECTOR2D& aPosition, double aRotation, const VECTOR3D& aOffset,
361 const VECTOR3D& aOrientation, TopLoc_Location& aLocation );
362
363 bool readIGES( Handle( TDocStd_Document ) & aDoc, const char* aFname );
364 bool readSTEP( Handle( TDocStd_Document ) & aDoc, const char* aFname );
365 bool readVRML( Handle( TDocStd_Document ) & aDoc, const char* aFname );
366
367 bool performMeshing( Handle( XCAFDoc_ShapeTool ) & aShapeTool );
368
369 TDF_Label transferModel( Handle( TDocStd_Document )& source, Handle( TDocStd_Document ) & dest,
370 const VECTOR3D& aScale );
371
372 bool CompressSTEP( wxString& inputFile, wxString& outputFile );
373
374 Handle( XCAFApp_Application ) m_app;
375 Handle( TDocStd_Document ) m_doc;
376 Handle( XCAFDoc_ShapeTool ) m_assy;
377 TDF_Label m_assy_label;
378 bool m_hasPCB; // set true if CreatePCB() has been invoked
379 bool m_simplifyShapes; // convert parts of outlines to arcs where possible
380 bool m_fuseShapes; // fuse geometry together
381 bool m_extraPadThickness; // add extra thickness to pads
382 std::vector<TDF_Label> m_pcb_labels; // labels for the PCB model (one by main outline)
383 MODEL_MAP m_models; // map of file names to model labels
384 int m_components; // number of successfully loaded components;
385 double m_precision; // model (length unit) numeric precision
386 double m_angleprec; // angle numeric precision
387 double m_copperColor[3]; // copper, RGB values
388 double m_padColor[3]; // pads, RGB values
389 BOARD_STACKUP m_stackup; // board stackup
390 LSET m_enabledLayers; // a set of layers enabled for export
391 wxString m_netFilter; // remove nets not matching this wildcard
392
393 double m_minx; // leftmost curve point
394 double m_mergeOCCMaxDist; // minimum distance (mm) below which two
395 // points are considered coincident by OCC
396
397 // Holes in copper and main outlines
398 std::vector<TopoDS_Shape> m_copperCutouts;
399 std::vector<TopoDS_Shape> m_boardCutouts;
400
401 // Main outlines (more than one board)
402 std::vector<TopoDS_Shape> m_board_outlines;
403
404 // Copper items. Key is netname.
405 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper;
406 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_pads;
407 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_vias;
408 std::map<wxString, std::vector<TopoDS_Shape>> m_board_copper_fused;
409
410 // Graphical items
411 std::vector<TopoDS_Shape> m_board_front_silk;
412 std::vector<TopoDS_Shape> m_board_back_silk;
413 std::vector<TopoDS_Shape> m_board_front_mask;
414 std::vector<TopoDS_Shape> m_board_back_mask;
415
416 // Extruded 3D bodies from footprint outlines
418 {
419 std::vector<TopoDS_Shape> bodyShapes;
420 std::vector<TopoDS_Shape> pinShapes;
421 wxString refDes;
422 uint32_t colorKey;
424 };
425
426 std::vector<EXTRUDED_BODY_ENTRY> m_extruded_bodies;
427
428 // Data for pads. Key example: Pad_F_U2_1_GND
429 std::map<wxString, std::vector<std::pair<gp_Pnt, TopoDS_Shape>>> m_pad_points;
430
432 wxString m_pcbName;
433
436
442};
443
444#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:73
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
bool AddExtrudedPins(const FOOTPRINT *aFootprint, const EXTRUDED_3D_BODY *aBody, bool aBottom, double aStandoff, const VECTOR2D &aOrigin)
Add pin extrusions for pad holes.
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)
double getStackupBodyThickness() const
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)
SYNC_REPORTER m_syncReporter
Thread-safe wrapper around the caller's reporter.
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
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)
A thread-safe REPORTER wrapper that serializes forwarding to an underlying reporter.
Definition reporter.h:184
std::string source
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:56
EXTRUSION_MATERIAL
Definition pcb_3d_body.h:31
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:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
VECTOR3< double > VECTOR3D
Definition vector3.h:230