2016-09-02 10:08:40 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2022-10-08 22:00:05 +00:00
|
|
|
* Copyright (C) 2022 Mark Roszko <mark.roszko@gmail.com>
|
2016-09-02 10:08:40 +00:00
|
|
|
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2023-03-02 18:38:19 +00:00
|
|
|
* Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-09-02 10:08:40 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <wx/filename.h>
|
2018-09-20 09:05:52 +00:00
|
|
|
#include <wx/filefn.h>
|
2020-08-26 02:27:08 +00:00
|
|
|
#include <wx/stdpaths.h>
|
|
|
|
#include <wx/wfstream.h>
|
2021-02-28 00:41:14 +00:00
|
|
|
#include <wx/zipstrm.h>
|
2020-08-26 02:27:08 +00:00
|
|
|
|
|
|
|
#include <decompress.hpp>
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
#include <footprint.h>
|
|
|
|
#include <pad.h>
|
|
|
|
|
|
|
|
#include "step_pcb_model.h"
|
2017-03-09 02:40:41 +00:00
|
|
|
#include "streamwrapper.h"
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
#include <IGESCAFControl_Reader.hxx>
|
|
|
|
#include <IGESCAFControl_Writer.hxx>
|
|
|
|
#include <IGESControl_Controller.hxx>
|
|
|
|
#include <IGESData_GlobalSection.hxx>
|
|
|
|
#include <IGESData_IGESModel.hxx>
|
|
|
|
#include <Interface_Static.hxx>
|
|
|
|
#include <Quantity_Color.hxx>
|
|
|
|
#include <STEPCAFControl_Reader.hxx>
|
|
|
|
#include <STEPCAFControl_Writer.hxx>
|
|
|
|
#include <APIHeaderSection_MakeHeader.hxx>
|
2018-01-26 00:38:29 +00:00
|
|
|
#include <Standard_Version.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <TCollection_ExtendedString.hxx>
|
|
|
|
#include <TDataStd_Name.hxx>
|
2021-09-12 21:41:18 +00:00
|
|
|
#include <TDataStd_TreeNode.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <TDF_LabelSequence.hxx>
|
|
|
|
#include <TDF_ChildIterator.hxx>
|
|
|
|
#include <TopExp_Explorer.hxx>
|
2021-09-12 21:41:18 +00:00
|
|
|
#include <XCAFDoc.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <XCAFDoc_DocumentTool.hxx>
|
|
|
|
#include <XCAFDoc_ColorTool.hxx>
|
|
|
|
|
|
|
|
#include <BRep_Tool.hxx>
|
|
|
|
#include <BRepMesh_IncrementalMesh.hxx>
|
2016-12-23 23:46:20 +00:00
|
|
|
#include <BRepBuilderAPI.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <BRepBuilderAPI_MakeEdge.hxx>
|
|
|
|
#include <BRepBuilderAPI_Transform.hxx>
|
2019-05-28 15:40:34 +00:00
|
|
|
#include <BRepBuilderAPI_GTransform.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <BRepBuilderAPI_MakeFace.hxx>
|
|
|
|
#include <BRepPrimAPI_MakePrism.hxx>
|
|
|
|
#include <BRepPrimAPI_MakeCylinder.hxx>
|
|
|
|
#include <BRepAlgoAPI_Cut.hxx>
|
|
|
|
|
|
|
|
#include <TopoDS.hxx>
|
|
|
|
#include <TopoDS_Wire.hxx>
|
|
|
|
#include <TopoDS_Face.hxx>
|
|
|
|
#include <TopoDS_Compound.hxx>
|
|
|
|
#include <TopoDS_Builder.hxx>
|
|
|
|
|
2018-06-18 12:20:25 +00:00
|
|
|
#include <Standard_Failure.hxx>
|
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
#include <gp_Ax2.hxx>
|
|
|
|
#include <gp_Circ.hxx>
|
|
|
|
#include <gp_Dir.hxx>
|
|
|
|
#include <gp_Pnt.hxx>
|
2020-10-21 08:18:03 +00:00
|
|
|
#include <Geom_BezierCurve.hxx>
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-08-27 17:02:55 +00:00
|
|
|
#include <macros.h>
|
|
|
|
|
2018-06-19 07:59:09 +00:00
|
|
|
static constexpr double USER_PREC = 1e-4;
|
|
|
|
static constexpr double USER_ANGLE_PREC = 1e-6;
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// nominal offset from the board
|
2018-06-19 07:59:09 +00:00
|
|
|
static constexpr double BOARD_OFFSET = 0.05;
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
// supported file types for 3D models
|
|
|
|
enum MODEL3D_FORMAT_TYPE
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2020-08-26 02:27:08 +00:00
|
|
|
FMT_NONE,
|
|
|
|
FMT_STEP,
|
|
|
|
FMT_STEPZ,
|
|
|
|
FMT_IGES,
|
|
|
|
FMT_EMN,
|
|
|
|
FMT_IDF,
|
|
|
|
FMT_WRL,
|
|
|
|
FMT_WRZ
|
2016-09-02 10:08:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
MODEL3D_FORMAT_TYPE fileType( const char* aFileName )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2017-03-09 02:40:41 +00:00
|
|
|
wxFileName lfile( wxString::FromUTF8Unchecked( aFileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
if( !lfile.FileExists() )
|
|
|
|
{
|
2020-02-15 09:58:44 +00:00
|
|
|
wxString msg;
|
2022-02-08 21:47:43 +00:00
|
|
|
msg.Printf( wxT( " * fileType(): no such file: %s\n" ),
|
2020-02-15 09:58:44 +00:00
|
|
|
wxString::FromUTF8Unchecked( aFileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2020-02-15 09:58:44 +00:00
|
|
|
ReportMessage( msg );
|
2016-09-02 10:08:40 +00:00
|
|
|
return FMT_NONE;
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:27:08 +00:00
|
|
|
wxString ext = lfile.GetExt().Lower();
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-02-08 21:47:43 +00:00
|
|
|
if( ext == wxT( "wrl" ) )
|
2017-11-04 12:44:13 +00:00
|
|
|
return FMT_WRL;
|
|
|
|
|
2022-02-08 21:47:43 +00:00
|
|
|
if( ext == wxT( "wrz" ) )
|
2020-08-26 02:27:08 +00:00
|
|
|
return FMT_WRZ;
|
|
|
|
|
2022-02-08 21:47:43 +00:00
|
|
|
if( ext == wxT( "idf" ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
return FMT_IDF; // component outline
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2022-02-08 21:47:43 +00:00
|
|
|
if( ext == wxT( "emn" ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
return FMT_EMN; // PCB assembly
|
|
|
|
|
2022-02-08 21:47:43 +00:00
|
|
|
if( ext == wxT( "stpz" ) || ext == wxT( "gz" ) )
|
2020-08-26 02:27:08 +00:00
|
|
|
return FMT_STEPZ;
|
|
|
|
|
2017-03-09 02:40:41 +00:00
|
|
|
OPEN_ISTREAM( ifile, aFileName );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2017-03-09 02:40:41 +00:00
|
|
|
if( ifile.fail() )
|
2016-09-02 10:08:40 +00:00
|
|
|
return FMT_NONE;
|
|
|
|
|
|
|
|
char iline[82];
|
|
|
|
memset( iline, 0, 82 );
|
|
|
|
ifile.getline( iline, 82 );
|
2017-03-09 02:40:41 +00:00
|
|
|
CLOSE_STREAM( ifile );
|
2016-09-02 10:08:40 +00:00
|
|
|
iline[81] = 0; // ensure NULL termination when string is too long
|
|
|
|
|
|
|
|
// check for STEP in Part 21 format
|
|
|
|
// (this can give false positives since Part 21 is not exclusively STEP)
|
|
|
|
if( !strncmp( iline, "ISO-10303-21;", 13 ) )
|
|
|
|
return FMT_STEP;
|
|
|
|
|
|
|
|
std::string fstr = iline;
|
|
|
|
|
|
|
|
// check for STEP in XML format
|
|
|
|
// (this can give both false positive and false negatives)
|
|
|
|
if( fstr.find( "urn:oid:1.0.10303." ) != std::string::npos )
|
|
|
|
return FMT_STEP;
|
|
|
|
|
|
|
|
// Note: this is a very simple test which can yield false positives; the only
|
|
|
|
// sure method for determining if a file *not* an IGES model is to attempt
|
|
|
|
// to load it.
|
|
|
|
if( iline[72] == 'S' && ( iline[80] == 0 || iline[80] == 13 || iline[80] == 10 ) )
|
|
|
|
return FMT_IGES;
|
|
|
|
|
|
|
|
return FMT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
STEP_PCB_MODEL::STEP_PCB_MODEL( const wxString& aPcbName )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
m_app = XCAFApp_Application::GetApplication();
|
|
|
|
m_app->NewDocument( "MDTV-XCAF", m_doc );
|
2023-03-06 10:47:29 +00:00
|
|
|
m_assy = XCAFDoc_DocumentTool::ShapeTool( m_doc->Main() );
|
2016-09-02 10:08:40 +00:00
|
|
|
m_assy_label = m_assy->NewShape();
|
|
|
|
m_hasPCB = false;
|
|
|
|
m_components = 0;
|
|
|
|
m_precision = USER_PREC;
|
|
|
|
m_angleprec = USER_ANGLE_PREC;
|
2023-03-02 18:38:19 +00:00
|
|
|
m_boardThickness = BOARD_THICKNESS_DEFAULT_MM;
|
|
|
|
m_copperThickness = COPPER_THICKNESS_DEFAULT_MM;
|
|
|
|
m_mergeOCCMaxDist = OCC_MAX_DISTANCE_TO_MERGE_POINTS;
|
2016-09-02 10:08:40 +00:00
|
|
|
m_minx = 1.0e10; // absurdly large number; any valid PCB X value will be smaller
|
2021-09-12 21:41:18 +00:00
|
|
|
m_pcbName = aPcbName;
|
2023-03-02 18:38:19 +00:00
|
|
|
BRepBuilderAPI::Precision( m_mergeOCCMaxDist );
|
|
|
|
m_maxError = pcbIUScale.mmToIU( ARC_TO_SEGMENT_MAX_ERROR_MM );
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
STEP_PCB_MODEL::~STEP_PCB_MODEL()
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
m_doc->Close();
|
|
|
|
}
|
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
bool STEP_PCB_MODEL::AddPadShape( const PAD* aPad, const VECTOR2D& aOrigin )
|
|
|
|
{
|
|
|
|
const std::shared_ptr<SHAPE_POLY_SET>& pad_shape = aPad->GetEffectivePolygon();
|
|
|
|
bool success = true;
|
2023-03-05 13:54:16 +00:00
|
|
|
VECTOR2I pos = aPad->GetPosition();
|
2023-03-02 18:38:19 +00:00
|
|
|
|
2023-03-05 13:54:16 +00:00
|
|
|
for( PCB_LAYER_ID pcb_layer = F_Cu; ; pcb_layer = B_Cu )
|
2023-03-02 18:38:19 +00:00
|
|
|
{
|
2023-03-05 13:54:16 +00:00
|
|
|
TopoDS_Shape curr_shape;
|
|
|
|
double Zpos = pcb_layer == F_Cu ? m_boardThickness : -m_copperThickness;
|
|
|
|
|
|
|
|
if( aPad->IsOnLayer( pcb_layer ) )
|
|
|
|
{
|
|
|
|
// Make a shape on top/bottom copper layer: a cylinder for rond shapes (pad or via)
|
|
|
|
// and a polygon for other shapes:
|
|
|
|
if( aPad->GetShape() == PAD_SHAPE::CIRCLE )
|
|
|
|
{
|
|
|
|
curr_shape = BRepPrimAPI_MakeCylinder(
|
|
|
|
pcbIUScale.IUTomm( aPad->GetSizeX() ) * 0.5, m_copperThickness ).Shape();
|
|
|
|
gp_Trsf shift;
|
|
|
|
shift.SetTranslation( gp_Vec( pcbIUScale.IUTomm( pos.x - aOrigin.x ),
|
|
|
|
-pcbIUScale.IUTomm( pos.y - aOrigin.y ),
|
|
|
|
Zpos ) );
|
|
|
|
BRepBuilderAPI_Transform round_shape( curr_shape, shift );
|
|
|
|
m_board_outlines.push_back( round_shape.Shape() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
success = MakeShape( curr_shape, pad_shape.get()->COutline(0), m_copperThickness, Zpos, aOrigin );
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
m_board_outlines.push_back( curr_shape );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( pcb_layer == B_Cu )
|
|
|
|
break;
|
2023-03-02 18:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !success ) // Error
|
|
|
|
ReportMessage( wxT( "OCC error adding pad/via polygon.\n" ) );
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool STEP_PCB_MODEL::AddCopperPolygonShapes( const SHAPE_POLY_SET* aPolyShapes, bool aOnTop, const VECTOR2D& aOrigin )
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
for( int ii = 0; ii < aPolyShapes->OutlineCount(); ii++ )
|
|
|
|
{
|
|
|
|
TopoDS_Shape copper_shape;
|
|
|
|
double z_pos = aOnTop ? m_boardThickness : -m_copperThickness;
|
|
|
|
|
|
|
|
if( MakeShape( copper_shape, aPolyShapes->COutline( ii ), m_copperThickness, z_pos, aOrigin ) )
|
|
|
|
{
|
|
|
|
m_board_outlines.push_back( copper_shape );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReportMessage( wxString::Format( wxT( "Could not add shape (%d points) to copper layer on %s.\n" ),
|
|
|
|
aPolyShapes->COutline( ii ).PointCount(),
|
|
|
|
aOnTop ? wxT( "top" ) : wxT( "bottom" ) ) );
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2022-11-13 14:22:46 +00:00
|
|
|
bool STEP_PCB_MODEL::AddPadHole( const PAD* aPad, const VECTOR2D& aOrigin )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2023-03-02 18:38:19 +00:00
|
|
|
if( aPad == nullptr || !aPad->GetDrillSize().x )
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
VECTOR2I pos = aPad->GetPosition();
|
2023-03-06 10:47:29 +00:00
|
|
|
const double margin = 0.01; // a small margin on the Z axix to be sure the hole
|
|
|
|
// is bigget than the board with copper
|
|
|
|
// must be > OCC_MAX_DISTANCE_TO_MERGE_POINTS
|
|
|
|
double holeZsize = m_boardThickness + ( m_copperThickness * 2 ) + ( margin * 2 );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_CIRCLE )
|
2022-07-06 21:32:52 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
TopoDS_Shape s =
|
2023-03-06 10:47:29 +00:00
|
|
|
BRepPrimAPI_MakeCylinder( pcbIUScale.IUTomm( aPad->GetDrillSize().x ) * 0.5, holeZsize ).Shape();
|
2016-09-02 10:08:40 +00:00
|
|
|
gp_Trsf shift;
|
2022-11-13 14:22:46 +00:00
|
|
|
shift.SetTranslation( gp_Vec( pcbIUScale.IUTomm( pos.x - aOrigin.x ),
|
2023-03-02 18:38:19 +00:00
|
|
|
-pcbIUScale.IUTomm( pos.y - aOrigin.y ),
|
2023-03-06 10:47:29 +00:00
|
|
|
-m_copperThickness - margin ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
BRepBuilderAPI_Transform hole( s, shift );
|
|
|
|
m_cutouts.push_back( hole.Shape() );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// slotted hole
|
2022-10-08 22:00:05 +00:00
|
|
|
SHAPE_POLY_SET holeOutlines;
|
2023-03-02 18:38:19 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( !aPad->TransformHoleToPolygon( holeOutlines, 0, m_maxError, ERROR_INSIDE ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
TopoDS_Shape hole;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( holeOutlines.OutlineCount() > 0 )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2023-03-06 10:47:29 +00:00
|
|
|
if( MakeShape( hole, holeOutlines.COutline( 0 ), holeZsize, -m_copperThickness - margin, aOrigin ) )
|
2022-10-08 22:00:05 +00:00
|
|
|
{
|
|
|
|
m_cutouts.push_back( hole );
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
2022-10-08 22:00:05 +00:00
|
|
|
else
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
return false;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
return true;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::AddComponent( const std::string& aFileNameUTF8, const std::string& aRefDes,
|
|
|
|
bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset,
|
|
|
|
VECTOR3D aOrientation, VECTOR3D aScale, bool aSubstituteModels )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2021-08-27 12:34:49 +00:00
|
|
|
if( aFileNameUTF8.empty() )
|
2018-03-19 11:17:09 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "No model defined for component %s.\n" ), aRefDes ) );
|
2018-03-19 11:17:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:02:55 +00:00
|
|
|
wxString fileName( wxString::FromUTF8( aFileNameUTF8.c_str() ) );
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Add component %s.\n" ), aRefDes ) );
|
2018-09-20 09:05:52 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// first retrieve a label
|
|
|
|
TDF_Label lmodel;
|
2022-10-08 22:00:05 +00:00
|
|
|
wxString errorMessage;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-08-27 12:34:49 +00:00
|
|
|
if( !getModelLabel( aFileNameUTF8, aScale, lmodel, aSubstituteModels, &errorMessage ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2021-07-08 15:22:33 +00:00
|
|
|
if( errorMessage.IsEmpty() )
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "No model for filename '%s'.\n" ), fileName ) );
|
2021-07-08 15:22:33 +00:00
|
|
|
else
|
|
|
|
ReportMessage( errorMessage );
|
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the Location transform
|
|
|
|
TopLoc_Location toploc;
|
|
|
|
|
|
|
|
if( !getModelLocation( aBottom, aPosition, aRotation, aOffset, aOrientation, toploc ) )
|
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
ReportMessage(
|
|
|
|
wxString::Format( wxT( "No location data for filename '%s'.\n" ), fileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the located sub-assembly
|
|
|
|
TDF_Label llabel = m_assy->AddComponent( m_assy_label, lmodel, toploc );
|
|
|
|
|
|
|
|
if( llabel.IsNull() )
|
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Could not add component with filename '%s'.\n" ),
|
2021-08-27 12:34:49 +00:00
|
|
|
fileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach the RefDes name
|
|
|
|
TCollection_ExtendedString refdes( aRefDes.c_str() );
|
|
|
|
TDataStd_Name::Set( llabel, refdes );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
void STEP_PCB_MODEL::SetPCBThickness( double aThickness )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
if( aThickness < 0.0 )
|
2023-03-02 18:38:19 +00:00
|
|
|
m_boardThickness = BOARD_THICKNESS_DEFAULT_MM;
|
|
|
|
else if( aThickness < BOARD_THICKNESS_MIN_MM )
|
|
|
|
m_boardThickness = BOARD_THICKNESS_MIN_MM;
|
2016-09-02 10:08:40 +00:00
|
|
|
else
|
2023-03-02 18:38:19 +00:00
|
|
|
m_boardThickness = aThickness;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
void STEP_PCB_MODEL::SetBoardColor( double r, double g, double b )
|
2022-05-09 20:02:33 +00:00
|
|
|
{
|
|
|
|
m_boardColor[0] = r;
|
|
|
|
m_boardColor[1] = g;
|
|
|
|
m_boardColor[2] = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-06 10:47:29 +00:00
|
|
|
void STEP_PCB_MODEL::SetCopperColor( double r, double g, double b )
|
|
|
|
{
|
|
|
|
m_copperColor[0] = r;
|
|
|
|
m_copperColor[1] = g;
|
|
|
|
m_copperColor[2] = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
void STEP_PCB_MODEL::OCCSetMergeMaxDistance( double aDistance )
|
2021-01-03 08:40:40 +00:00
|
|
|
{
|
2022-10-07 16:53:31 +00:00
|
|
|
// Ensure a minimal value (in mm)
|
2023-03-02 18:38:19 +00:00
|
|
|
m_mergeOCCMaxDist = aDistance;
|
|
|
|
BRepBuilderAPI::Precision( m_mergeOCCMaxDist );
|
2021-01-03 08:40:40 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
bool STEP_PCB_MODEL::isBoardOutlineValid()
|
|
|
|
{
|
|
|
|
return m_pcb_labels.size() > 0;
|
|
|
|
}
|
|
|
|
|
2021-01-03 08:40:40 +00:00
|
|
|
|
2022-11-13 14:22:46 +00:00
|
|
|
bool STEP_PCB_MODEL::MakeShape( TopoDS_Shape& aShape, const SHAPE_LINE_CHAIN& aChain,
|
2023-03-02 18:38:19 +00:00
|
|
|
double aThickness, double aZposition, const VECTOR2D& aOrigin )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
if( !aShape.IsNull() )
|
|
|
|
return false; // there is already data in the shape object
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( !aChain.IsClosed() )
|
|
|
|
return false; // the loop is not closed
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
BRepBuilderAPI_MakeWire wire;
|
2023-03-02 18:38:19 +00:00
|
|
|
bool success = true;
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2022-11-30 13:28:48 +00:00
|
|
|
gp_Pnt start = gp_Pnt( pcbIUScale.IUTomm( aChain.CPoint( 0 ).x - aOrigin.x ),
|
2023-03-02 18:38:19 +00:00
|
|
|
-pcbIUScale.IUTomm( aChain.CPoint( 0 ).y - aOrigin.y ), aZposition );
|
|
|
|
|
|
|
|
int items_count = 0;
|
2022-11-30 13:28:48 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
for( int j = 0; j < aChain.PointCount(); j++ )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-11-30 13:28:48 +00:00
|
|
|
int next = j+1;
|
2022-10-08 22:00:05 +00:00
|
|
|
|
|
|
|
gp_Pnt end;
|
2022-11-30 13:28:48 +00:00
|
|
|
|
|
|
|
if( next >= aChain.PointCount() )
|
2023-03-02 18:38:19 +00:00
|
|
|
next = 0;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
end = gp_Pnt( pcbIUScale.IUTomm( aChain.CPoint( next ).x - aOrigin.x ),
|
|
|
|
-pcbIUScale.IUTomm( aChain.CPoint( next ).y - aOrigin.y ), aZposition );
|
2022-11-30 13:28:48 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
// Do not export too short segments: they create broken shape because OCC thinks
|
|
|
|
// start point and end point are at the same place
|
|
|
|
double seg_len = std::hypot( end.X() - start.X(), end.Y() - start.Y() );
|
2022-11-30 13:28:48 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
if( seg_len <= m_mergeOCCMaxDist )
|
|
|
|
continue;
|
2022-10-08 22:00:05 +00:00
|
|
|
try
|
|
|
|
{
|
2023-03-02 18:38:19 +00:00
|
|
|
TopoDS_Edge edge;
|
2022-10-08 22:00:05 +00:00
|
|
|
edge = BRepBuilderAPI_MakeEdge( start, end );
|
|
|
|
wire.Add( edge );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
if( wire.Error() != BRepBuilderAPI_WireDone )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
ReportMessage( wxT( "failed to add curve\n" ) );
|
|
|
|
return false;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
2023-03-02 18:38:19 +00:00
|
|
|
|
|
|
|
items_count++;
|
2022-10-08 22:00:05 +00:00
|
|
|
}
|
|
|
|
catch( const Standard_Failure& e )
|
|
|
|
{
|
2023-03-02 18:38:19 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "MakeShape1: OCC exception: %s\n" ),
|
|
|
|
e.GetMessageString() ) );
|
2022-10-08 22:00:05 +00:00
|
|
|
success = false;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
ReportMessage( wxS( "failed to add edge\n" ) );
|
|
|
|
return false;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
2022-11-30 13:28:48 +00:00
|
|
|
|
|
|
|
start = end;
|
2022-10-08 22:00:05 +00:00
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-11-30 13:28:48 +00:00
|
|
|
BRepBuilderAPI_MakeFace face;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
face = BRepBuilderAPI_MakeFace( wire );
|
|
|
|
}
|
|
|
|
catch( const Standard_Failure& e )
|
|
|
|
{
|
|
|
|
ReportMessage(
|
2023-03-02 18:38:19 +00:00
|
|
|
wxString::Format( wxT( "MakeShape2 (items_count %d): OCC exception: %s\n" ),
|
|
|
|
items_count, e.GetMessageString() ) );
|
2022-11-30 13:28:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
aShape = BRepPrimAPI_MakePrism( face, gp_Vec( 0, 0, aThickness ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
if( aShape.IsNull() )
|
|
|
|
{
|
|
|
|
ReportMessage( wxT( "failed to create a prismatic shape\n" ) );
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-06-18 12:20:25 +00:00
|
|
|
|
|
|
|
|
2022-11-13 14:22:46 +00:00
|
|
|
bool STEP_PCB_MODEL::CreatePCB( SHAPE_POLY_SET& aOutline, VECTOR2D aOrigin )
|
2022-10-08 22:00:05 +00:00
|
|
|
{
|
|
|
|
if( m_hasPCB )
|
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
if( !isBoardOutlineValid() )
|
2022-10-08 22:00:05 +00:00
|
|
|
return false;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
return true;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
m_hasPCB = true; // whether or not operations fail we note that CreatePCB has been invoked
|
2022-11-15 15:37:00 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
// Number of items having the copper color
|
|
|
|
int copper_item_count = m_board_outlines.size();
|
2022-10-08 22:00:05 +00:00
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
// Support for more than one main outline (more than one board)
|
2022-10-08 22:00:05 +00:00
|
|
|
for( int cnt = 0; cnt < aOutline.OutlineCount(); cnt++ )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
const SHAPE_LINE_CHAIN& outline = aOutline.COutline( cnt );
|
|
|
|
|
2022-11-30 13:28:48 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Build board main outline %d with %d points.\n" ),
|
|
|
|
cnt+1, outline.PointCount() ) );
|
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
TopoDS_Shape curr_brd;
|
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
if( !MakeShape( curr_brd, outline, m_boardThickness, 0.0, aOrigin ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
// Error
|
2022-11-30 13:28:48 +00:00
|
|
|
ReportMessage( wxString::Format(
|
|
|
|
wxT( "OCC error adding main outline polygon %d with %d points.\n" ),
|
|
|
|
cnt+1, outline.PointCount() ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
2022-11-15 15:37:00 +00:00
|
|
|
else
|
2023-03-02 18:38:19 +00:00
|
|
|
m_board_outlines.push_back( curr_brd );
|
2022-10-08 22:00:05 +00:00
|
|
|
|
2022-11-30 13:28:48 +00:00
|
|
|
// Generate board cutouts in current main outline:
|
|
|
|
if( aOutline.HoleCount( cnt ) > 0 )
|
|
|
|
{
|
|
|
|
ReportMessage( wxString::Format( wxT( "Add cutouts in outline %d (%d cutout(s)).\n" ),
|
|
|
|
cnt+1, aOutline.HoleCount( cnt ) ) );
|
|
|
|
}
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
for( int ii = 0; ii < aOutline.HoleCount( cnt ); ii++ )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-10-08 22:00:05 +00:00
|
|
|
const SHAPE_LINE_CHAIN& holeOutline = aOutline.Hole( cnt, ii );
|
2016-09-02 10:08:40 +00:00
|
|
|
TopoDS_Shape hole;
|
|
|
|
|
2023-03-02 18:38:19 +00:00
|
|
|
if( MakeShape( hole, holeOutline, m_boardThickness + (m_copperThickness*4),
|
|
|
|
-m_copperThickness*2, aOrigin ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
m_cutouts.push_back( hole );
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 14:03:08 +00:00
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
// subtract cutouts (if any)
|
2018-10-02 06:34:07 +00:00
|
|
|
if( m_cutouts.size() )
|
2021-07-06 20:22:06 +00:00
|
|
|
{
|
2022-11-30 13:28:48 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Build board cutouts and holes (%d hole(s)).\n" ),
|
2022-02-08 21:47:43 +00:00
|
|
|
(int) m_cutouts.size() ) );
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2020-02-15 09:58:44 +00:00
|
|
|
TopTools_ListOfShape holelist;
|
2018-10-02 06:34:07 +00:00
|
|
|
|
2022-11-14 08:02:40 +00:00
|
|
|
for( TopoDS_Shape& hole : m_cutouts )
|
2022-10-08 22:00:05 +00:00
|
|
|
holelist.Append( hole );
|
2020-02-15 09:58:44 +00:00
|
|
|
|
2023-03-06 15:35:54 +00:00
|
|
|
// Remove holes for each item (board body or bodies, one can have more than one board)
|
|
|
|
// and copper items (copper_item_count items)
|
2023-03-02 18:38:19 +00:00
|
|
|
int cnt = 0;
|
|
|
|
for( TopoDS_Shape& board: m_board_outlines )
|
2022-11-15 15:37:00 +00:00
|
|
|
{
|
2023-03-02 18:38:19 +00:00
|
|
|
cnt++;
|
|
|
|
|
|
|
|
if( cnt % 10 == 0 )
|
|
|
|
ReportMessage( wxString::Format( wxT( "added %d/%d shapes\n" ),
|
|
|
|
cnt, (int)m_board_outlines.size() ) );
|
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
TopTools_ListOfShape mainbrd;
|
|
|
|
mainbrd.Append( board );
|
|
|
|
|
2023-03-21 08:30:11 +00:00
|
|
|
BRepAlgoAPI_Cut Cut;
|
2022-11-15 15:37:00 +00:00
|
|
|
Cut.SetArguments( mainbrd );
|
|
|
|
|
|
|
|
Cut.SetTools( holelist );
|
|
|
|
Cut.Build();
|
|
|
|
|
|
|
|
board = Cut.Shape();
|
|
|
|
}
|
2020-02-15 09:58:44 +00:00
|
|
|
}
|
2020-04-22 08:43:28 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// push the board to the data structure
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxT( "\nGenerate board full shape.\n" ) );
|
2021-09-11 04:27:48 +00:00
|
|
|
|
2021-09-14 01:17:22 +00:00
|
|
|
// Dont expand the component or else coloring it gets hard
|
2023-03-02 18:38:19 +00:00
|
|
|
for( TopoDS_Shape& board: m_board_outlines )
|
2022-11-15 15:37:00 +00:00
|
|
|
{
|
|
|
|
m_pcb_labels.push_back( m_assy->AddComponent( m_assy_label, board, false ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
if( m_pcb_labels.back().IsNull() )
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-07-06 21:32:52 +00:00
|
|
|
// AddComponent adds a label that has a reference (not a parent/child relation) to the real
|
|
|
|
// label. We need to extract that real label to name it for the STEP output cleanly
|
2021-09-12 21:41:18 +00:00
|
|
|
// Why are we trying to name the bare board? Because CAD tools like SolidWorks do fun things
|
2022-07-06 21:32:52 +00:00
|
|
|
// like "deduplicate" imported STEPs by swapping STEP assembly components with already
|
|
|
|
// identically named assemblies. So we want to avoid having the PCB be generally defaulted
|
|
|
|
// to "Component" or "Assembly".
|
2021-09-12 21:41:18 +00:00
|
|
|
|
2023-03-06 10:47:29 +00:00
|
|
|
// Init colors for the board body and the copper items (if any)
|
2022-11-15 15:37:00 +00:00
|
|
|
Handle( XCAFDoc_ColorTool ) colorTool = XCAFDoc_DocumentTool::ColorTool( m_doc->Main() );
|
2023-03-06 10:47:29 +00:00
|
|
|
Quantity_Color board_color( m_boardColor[0], m_boardColor[1], m_boardColor[2],
|
|
|
|
Quantity_TOC_RGB );
|
|
|
|
Quantity_Color copper_color( m_copperColor[0], m_copperColor[1], m_copperColor[2],
|
|
|
|
Quantity_TOC_RGB );
|
2022-11-15 15:37:00 +00:00
|
|
|
|
|
|
|
int pcbIdx = 1;
|
2023-03-02 18:38:19 +00:00
|
|
|
int copper_objects_cnt = 0;
|
2022-11-15 15:37:00 +00:00
|
|
|
|
|
|
|
for( TDF_Label& pcb_label : m_pcb_labels )
|
2021-09-12 21:41:18 +00:00
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
Handle( TDataStd_TreeNode ) node;
|
|
|
|
|
|
|
|
if( pcb_label.FindAttribute( XCAFDoc::ShapeRefGUID(), node ) )
|
2021-09-12 21:41:18 +00:00
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
// Gives a name to each board object
|
|
|
|
TDF_Label label = node->Father()->Label();
|
2021-09-12 21:41:18 +00:00
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
if( !label.IsNull() )
|
|
|
|
{
|
|
|
|
wxString pcbName;
|
2022-05-09 20:02:33 +00:00
|
|
|
|
2023-03-21 08:30:11 +00:00
|
|
|
// Note, we include the pcb/project name as a prefix
|
|
|
|
// because several STEP importing CAD software like SolidWorks
|
|
|
|
// will deduplicate anything imported by it's STEP name
|
|
|
|
|
2023-03-06 10:47:29 +00:00
|
|
|
if( copper_objects_cnt < copper_item_count )
|
|
|
|
{
|
2023-03-21 08:30:11 +00:00
|
|
|
pcbName = wxString::Format( wxT( "%s_Copper_Item%d" ),
|
|
|
|
m_pcbName, copper_objects_cnt+1 );
|
2023-03-06 10:47:29 +00:00
|
|
|
}
|
2022-11-15 15:37:00 +00:00
|
|
|
else
|
2023-03-06 10:47:29 +00:00
|
|
|
{
|
|
|
|
if( m_pcb_labels.size() == 1 )
|
2023-03-21 08:30:11 +00:00
|
|
|
pcbName = wxString::Format( wxT( "%s_PCB" ), m_pcbName );
|
2023-03-06 10:47:29 +00:00
|
|
|
else
|
2023-03-21 08:30:11 +00:00
|
|
|
pcbName = wxString::Format( wxT( "%s_PCB%d" ), m_pcbName, pcbIdx++ );
|
2023-03-06 10:47:29 +00:00
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
std::string pcbNameStdString( pcbName.ToUTF8() );
|
|
|
|
TCollection_ExtendedString partname( pcbNameStdString.c_str() );
|
|
|
|
TDataStd_Name::Set( label, partname );
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
// color the PCB
|
2023-03-02 18:38:19 +00:00
|
|
|
TopExp_Explorer topex;
|
2023-03-06 15:35:54 +00:00
|
|
|
topex.Init( m_assy->GetShape( pcb_label ), TopAbs_SOLID );
|
2022-11-15 15:37:00 +00:00
|
|
|
|
|
|
|
while( topex.More() )
|
|
|
|
{
|
2023-03-06 10:47:29 +00:00
|
|
|
// First objects are copper objects, last(s) is the board body
|
2023-03-02 18:38:19 +00:00
|
|
|
if( copper_objects_cnt < copper_item_count )
|
|
|
|
colorTool->SetColor( topex.Current(), copper_color, XCAFDoc_ColorSurf );
|
|
|
|
else
|
|
|
|
colorTool->SetColor( topex.Current(), board_color, XCAFDoc_ColorSurf );
|
|
|
|
|
2022-11-15 15:37:00 +00:00
|
|
|
topex.Next();
|
|
|
|
}
|
2023-03-02 18:38:19 +00:00
|
|
|
|
|
|
|
copper_objects_cnt++;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
#if( defined OCC_VERSION_HEX ) && ( OCC_VERSION_HEX > 0x070101 )
|
2018-01-26 00:38:29 +00:00
|
|
|
m_assy->UpdateAssemblies();
|
|
|
|
#endif
|
2022-07-06 21:32:52 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SUPPORTS_IGES
|
|
|
|
// write the assembly model in IGES format
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::WriteIGES( const wxString& aFileName )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
if( !isBoardOutlineValid() )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "No valid PCB assembly; cannot create output file "
|
|
|
|
"'%s'.\n" ),
|
2021-07-06 20:22:06 +00:00
|
|
|
aFileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxFileName fn( aFileName );
|
|
|
|
IGESControl_Controller::Init();
|
|
|
|
IGESCAFControl_Writer writer;
|
|
|
|
writer.SetColorMode( Standard_True );
|
|
|
|
writer.SetNameMode( Standard_True );
|
|
|
|
IGESData_GlobalSection header = writer.Model()->GlobalSection();
|
2020-02-15 09:58:44 +00:00
|
|
|
header.SetFileName( new TCollection_HAsciiString( fn.GetFullName().ToAscii() ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
header.SetSendName( new TCollection_HAsciiString( "KiCad electronic assembly" ) );
|
2022-07-06 21:32:52 +00:00
|
|
|
header.SetAuthorName(
|
|
|
|
new TCollection_HAsciiString( Interface_Static::CVal( "write.iges.header.author" ) ) );
|
|
|
|
header.SetCompanyName(
|
|
|
|
new TCollection_HAsciiString( Interface_Static::CVal( "write.iges.header.company" ) ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
writer.Model()->SetGlobalSection( header );
|
|
|
|
|
|
|
|
if( Standard_False == writer.Perform( m_doc, aFileName.c_str() ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::WriteSTEP( const wxString& aFileName )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-11-15 15:37:00 +00:00
|
|
|
if( !isBoardOutlineValid() )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "No valid PCB assembly; cannot create output file "
|
|
|
|
"'%s'.\n" ),
|
2021-07-06 20:22:06 +00:00
|
|
|
aFileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-06 21:32:52 +00:00
|
|
|
|
2021-03-03 14:26:20 +00:00
|
|
|
wxFileName fn( aFileName );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
STEPCAFControl_Writer writer;
|
|
|
|
writer.SetColorMode( Standard_True );
|
|
|
|
writer.SetNameMode( Standard_True );
|
|
|
|
|
2021-03-03 14:26:20 +00:00
|
|
|
// This must be set before we "transfer" the document.
|
|
|
|
// Should default to kicad_pcb.general.title_block.title,
|
|
|
|
// but in the meantime, defaulting to the basename of the output
|
|
|
|
// target is still better than "open cascade step translter v..."
|
|
|
|
// UTF8 should be ok from ISO 10303-21:2016, but... older stuff? use boring ascii
|
2021-07-06 20:22:06 +00:00
|
|
|
if( !Interface_Static::SetCVal( "write.step.product.name", fn.GetName().ToAscii() ) )
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxT( "Failed to set step product name, but will attempt to continue." ) );
|
2021-03-03 14:26:20 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
if( Standard_False == writer.Transfer( m_doc, STEPControl_AsIs ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
APIHeaderSection_MakeHeader hdr( writer.ChangeWriter().Model() );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2020-02-15 09:58:44 +00:00
|
|
|
// Note: use only Ascii7 chars, non Ascii7 chars (therefore UFT8 chars)
|
|
|
|
// are creating issues in the step file
|
|
|
|
hdr.SetName( new TCollection_HAsciiString( fn.GetFullName().ToAscii() ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// TODO: how to control and ensure consistency with IGES?
|
2020-02-15 09:58:44 +00:00
|
|
|
hdr.SetAuthorValue( 1, new TCollection_HAsciiString( "Pcbnew" ) );
|
|
|
|
hdr.SetOrganizationValue( 1, new TCollection_HAsciiString( "Kicad" ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
hdr.SetOriginatingSystem( new TCollection_HAsciiString( "KiCad to STEP converter" ) );
|
|
|
|
hdr.SetDescriptionValue( 1, new TCollection_HAsciiString( "KiCad electronic assembly" ) );
|
|
|
|
|
2018-09-20 09:05:52 +00:00
|
|
|
bool success = true;
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
// Creates a temporary file with a ascii7 name, because writer does not know unicode filenames.
|
2018-09-20 09:05:52 +00:00
|
|
|
wxString currCWD = wxGetCwd();
|
2021-07-29 11:10:58 +00:00
|
|
|
wxString workCWD = fn.GetPath();
|
2020-02-15 09:58:44 +00:00
|
|
|
|
|
|
|
if( !workCWD.IsEmpty() )
|
|
|
|
wxSetWorkingDirectory( workCWD );
|
|
|
|
|
2018-09-20 09:05:52 +00:00
|
|
|
char tmpfname[] = "$tempfile$.step";
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2018-09-20 09:05:52 +00:00
|
|
|
if( Standard_False == writer.Write( tmpfname ) )
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
if( success )
|
|
|
|
{
|
2020-07-30 08:40:51 +00:00
|
|
|
if( !wxRenameFile( tmpfname, fn.GetFullName(), true ) )
|
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Cannot rename temporary file '%s' to '%s'.\n" ),
|
2021-07-06 20:22:06 +00:00
|
|
|
tmpfname,
|
|
|
|
fn.GetFullName() ) );
|
2020-07-30 08:40:51 +00:00
|
|
|
success = false;
|
|
|
|
}
|
2018-09-20 09:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxSetWorkingDirectory( currCWD );
|
|
|
|
|
|
|
|
return success;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::getModelLabel( const std::string& aFileNameUTF8, VECTOR3D aScale, TDF_Label& aLabel,
|
2021-07-08 15:22:33 +00:00
|
|
|
bool aSubstituteModels, wxString* aErrorMessage )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2021-08-27 12:34:49 +00:00
|
|
|
std::string model_key = aFileNameUTF8 + "_" + std::to_string( aScale.x )
|
2020-02-15 09:58:44 +00:00
|
|
|
+ "_" + std::to_string( aScale.y ) + "_" + std::to_string( aScale.z );
|
2019-05-28 15:40:34 +00:00
|
|
|
|
|
|
|
MODEL_MAP::const_iterator mm = m_models.find( model_key );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
if( mm != m_models.end() )
|
|
|
|
{
|
|
|
|
aLabel = mm->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
aLabel.Nullify();
|
|
|
|
|
|
|
|
Handle( TDocStd_Document ) doc;
|
|
|
|
m_app->NewDocument( "MDTV-XCAF", doc );
|
|
|
|
|
2021-08-27 17:02:55 +00:00
|
|
|
wxString fileName( wxString::FromUTF8( aFileNameUTF8.c_str() ) );
|
2023-03-02 18:38:19 +00:00
|
|
|
MODEL3D_FORMAT_TYPE modelFmt = fileType( aFileNameUTF8.c_str() );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
switch( modelFmt )
|
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
case FMT_IGES:
|
2021-08-27 12:34:49 +00:00
|
|
|
if( !readIGES( doc, aFileNameUTF8.c_str() ) )
|
2021-07-29 11:10:58 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "readIGES() failed on filename '%s'.\n" ),
|
|
|
|
fileName ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
case FMT_STEP:
|
2021-08-27 12:34:49 +00:00
|
|
|
if( !readSTEP( doc, aFileNameUTF8.c_str() ) )
|
2020-08-26 02:27:08 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "readSTEP() failed on filename '%s'.\n" ),
|
|
|
|
fileName ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
case FMT_STEPZ:
|
|
|
|
{
|
2021-08-27 12:34:49 +00:00
|
|
|
// To export a compressed step file (.stpz or .stp.gz file), the best way is to
|
|
|
|
// decaompress it in a temporaty file and load this temporary file
|
|
|
|
wxFFileInputStream ifile( fileName );
|
|
|
|
wxFileName outFile( fileName );
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
outFile.SetPath( wxStandardPaths::Get().GetTempDir() );
|
2022-02-08 21:47:43 +00:00
|
|
|
outFile.SetExt( wxT( "step" ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
wxFileOffset size = ifile.GetLength();
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( size == wxInvalidOffset )
|
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "getModelLabel() failed on filename '%s'.\n" ),
|
2021-08-27 12:34:49 +00:00
|
|
|
fileName ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
{
|
|
|
|
bool success = false;
|
|
|
|
wxFFileOutputStream ofile( outFile.GetFullPath() );
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( !ofile.IsOk() )
|
|
|
|
return false;
|
2020-08-26 02:27:08 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
char* buffer = new char[size];
|
2020-08-26 21:18:54 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
ifile.Read( buffer, size );
|
|
|
|
std::string expanded;
|
2021-02-28 00:41:14 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
expanded = gzip::decompress( buffer, size );
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "failed to decompress '%s'.\n" ),
|
|
|
|
fileName ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
}
|
2021-02-28 00:41:14 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( expanded.empty() )
|
|
|
|
{
|
|
|
|
ifile.Reset();
|
|
|
|
ifile.SeekI( 0 );
|
|
|
|
wxZipInputStream izipfile( ifile );
|
|
|
|
std::unique_ptr<wxZipEntry> zip_file( izipfile.GetNextEntry() );
|
|
|
|
|
|
|
|
if( zip_file && !zip_file->IsDir() && izipfile.CanRead() )
|
2021-02-28 00:41:14 +00:00
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
izipfile.Read( ofile );
|
|
|
|
success = true;
|
2020-08-26 21:18:54 +00:00
|
|
|
}
|
2021-07-29 11:10:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ofile.Write( expanded.data(), expanded.size() );
|
2020-08-26 02:27:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
delete[] buffer;
|
|
|
|
ofile.Close();
|
|
|
|
|
2021-08-27 12:34:49 +00:00
|
|
|
if( success )
|
|
|
|
{
|
2021-08-27 17:02:55 +00:00
|
|
|
std::string altFileNameUTF8 = TO_UTF8( outFile.GetFullPath() );
|
2022-10-08 22:00:05 +00:00
|
|
|
success =
|
|
|
|
getModelLabel( altFileNameUTF8, VECTOR3D( 1.0, 1.0, 1.0 ), aLabel, false );
|
2021-08-27 12:34:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
return success;
|
2020-08-26 02:27:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FMT_WRL:
|
|
|
|
case FMT_WRZ:
|
|
|
|
/* WRL files are preferred for internal rendering, due to superior material properties, etc.
|
|
|
|
* However they are not suitable for MCAD export.
|
|
|
|
*
|
|
|
|
* If a .wrl file is specified, attempt to locate a replacement file for it.
|
|
|
|
*
|
|
|
|
* If a valid replacement file is found, the label for THAT file will be associated with
|
|
|
|
* the .wrl file
|
|
|
|
*/
|
|
|
|
if( aSubstituteModels )
|
|
|
|
{
|
2021-08-27 12:34:49 +00:00
|
|
|
wxFileName wrlName( fileName );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
wxString basePath = wrlName.GetPath();
|
|
|
|
wxString baseName = wrlName.GetName();
|
|
|
|
|
|
|
|
// List of alternate files to look for
|
|
|
|
// Given in order of preference
|
|
|
|
// (Break if match is found)
|
|
|
|
wxArrayString alts;
|
|
|
|
|
|
|
|
// Step files
|
2022-02-08 21:47:43 +00:00
|
|
|
alts.Add( wxT( "stp" ) );
|
|
|
|
alts.Add( wxT( "step" ) );
|
|
|
|
alts.Add( wxT( "STP" ) );
|
|
|
|
alts.Add( wxT( "STEP" ) );
|
|
|
|
alts.Add( wxT( "Stp" ) );
|
|
|
|
alts.Add( wxT( "Step" ) );
|
|
|
|
alts.Add( wxT( "stpz" ) );
|
|
|
|
alts.Add( wxT( "stpZ" ) );
|
|
|
|
alts.Add( wxT( "STPZ" ) );
|
|
|
|
alts.Add( wxT( "step.gz" ) );
|
|
|
|
alts.Add( wxT( "stp.gz" ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
// IGES files
|
2022-02-08 21:47:43 +00:00
|
|
|
alts.Add( wxT( "iges" ) );
|
|
|
|
alts.Add( wxT( "IGES" ) );
|
|
|
|
alts.Add( wxT( "igs" ) );
|
|
|
|
alts.Add( wxT( "IGS" ) );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
//TODO - Other alternative formats?
|
|
|
|
|
|
|
|
for( const auto& alt : alts )
|
2017-11-04 12:44:13 +00:00
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
wxFileName altFile( basePath, baseName + wxT( "." ) + alt );
|
2017-11-04 12:44:13 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
if( altFile.IsOk() && altFile.FileExists() )
|
|
|
|
{
|
2021-08-27 17:02:55 +00:00
|
|
|
std::string altFileNameUTF8 = TO_UTF8( altFile.GetFullPath() );
|
2021-07-29 11:10:58 +00:00
|
|
|
|
|
|
|
// When substituting a STEP/IGS file for VRML, do not apply the VRML scaling
|
|
|
|
// to the new STEP model. This process of auto-substitution is janky as all
|
|
|
|
// heck so let's not mix up un-displayed scale factors with potentially
|
|
|
|
// mis-matched files. And hope that the user doesn't have multiples files
|
|
|
|
// named "model.wrl" and "model.stp" referring to different parts.
|
|
|
|
// TODO: Fix model handling in v7. Default models should only be STP.
|
|
|
|
// Have option to override this in DISPLAY.
|
2022-10-08 22:00:05 +00:00
|
|
|
if( getModelLabel( altFileNameUTF8, VECTOR3D( 1.0, 1.0, 1.0 ), aLabel, false ) )
|
2017-11-04 12:44:13 +00:00
|
|
|
{
|
2021-07-29 11:10:58 +00:00
|
|
|
return true;
|
2017-11-04 12:44:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
return false; // No replacement model found
|
|
|
|
}
|
|
|
|
else // Substitution is not allowed
|
|
|
|
{
|
|
|
|
if( aErrorMessage )
|
2022-02-08 21:47:43 +00:00
|
|
|
aErrorMessage->Printf( wxT( "Cannot add a VRML model to a STEP file.\n" ) );
|
2021-08-14 10:07:58 +00:00
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2017-11-04 12:44:13 +00:00
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// TODO: implement IDF and EMN converters
|
|
|
|
|
2021-07-29 11:10:58 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2016-09-02 10:08:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 15:40:34 +00:00
|
|
|
aLabel = transferModel( doc, m_doc, aScale );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
if( aLabel.IsNull() )
|
|
|
|
{
|
2022-02-08 21:47:43 +00:00
|
|
|
ReportMessage( wxString::Format( wxT( "Could not transfer model data from file '%s'.\n" ),
|
2021-08-27 12:34:49 +00:00
|
|
|
fileName ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attach the PART NAME ( base filename: note that in principle
|
|
|
|
// different models may have the same base filename )
|
2021-08-27 12:34:49 +00:00
|
|
|
wxFileName afile( fileName );
|
2016-09-02 10:08:40 +00:00
|
|
|
std::string pname( afile.GetName().ToUTF8() );
|
|
|
|
TCollection_ExtendedString partname( pname.c_str() );
|
|
|
|
TDataStd_Name::Set( aLabel, partname );
|
|
|
|
|
2019-05-28 15:40:34 +00:00
|
|
|
m_models.insert( MODEL_DATUM( model_key, aLabel ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
++m_components;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::getModelLocation( bool aBottom, VECTOR2D aPosition, double aRotation, VECTOR3D aOffset, VECTOR3D aOrientation,
|
|
|
|
TopLoc_Location& aLocation )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
// Order of operations:
|
|
|
|
// a. aOrientation is applied -Z*-Y*-X
|
|
|
|
// b. aOffset is applied
|
|
|
|
// Top ? add thickness to the Z offset
|
|
|
|
// c. Bottom ? Rotate on X axis (in contrast to most ECAD which mirror on Y),
|
|
|
|
// then rotate on +Z
|
|
|
|
// Top ? rotate on -Z
|
|
|
|
// d. aPosition is applied
|
|
|
|
//
|
|
|
|
// Note: Y axis is inverted in KiCad
|
|
|
|
|
|
|
|
gp_Trsf lPos;
|
|
|
|
lPos.SetTranslation( gp_Vec( aPosition.x, -aPosition.y, 0.0 ) );
|
|
|
|
|
2017-11-08 09:56:57 +00:00
|
|
|
// Offset board thickness
|
|
|
|
aOffset.z += BOARD_OFFSET;
|
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
gp_Trsf lRot;
|
|
|
|
|
|
|
|
if( aBottom )
|
|
|
|
{
|
|
|
|
lRot.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 0.0, 0.0, 1.0 ) ), aRotation );
|
|
|
|
lPos.Multiply( lRot );
|
|
|
|
lRot.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 1.0, 0.0, 0.0 ) ), M_PI );
|
|
|
|
lPos.Multiply( lRot );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-02 18:38:19 +00:00
|
|
|
aOffset.z += m_boardThickness;
|
2016-09-02 10:08:40 +00:00
|
|
|
lRot.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 0.0, 0.0, 1.0 ) ), aRotation );
|
|
|
|
lPos.Multiply( lRot );
|
|
|
|
}
|
|
|
|
|
|
|
|
gp_Trsf lOff;
|
|
|
|
lOff.SetTranslation( gp_Vec( aOffset.x, aOffset.y, aOffset.z ) );
|
|
|
|
lPos.Multiply( lOff );
|
|
|
|
|
|
|
|
gp_Trsf lOrient;
|
2021-09-04 17:05:30 +00:00
|
|
|
lOrient.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 0.0, 0.0, 1.0 ) ),
|
|
|
|
-aOrientation.z );
|
2016-09-02 10:08:40 +00:00
|
|
|
lPos.Multiply( lOrient );
|
2021-09-04 17:05:30 +00:00
|
|
|
lOrient.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 0.0, 1.0, 0.0 ) ),
|
|
|
|
-aOrientation.y );
|
2016-09-02 10:08:40 +00:00
|
|
|
lPos.Multiply( lOrient );
|
2021-09-04 17:05:30 +00:00
|
|
|
lOrient.SetRotation( gp_Ax1( gp_Pnt( 0.0, 0.0, 0.0 ), gp_Dir( 1.0, 0.0, 0.0 ) ),
|
|
|
|
-aOrientation.x );
|
2016-09-02 10:08:40 +00:00
|
|
|
lPos.Multiply( lOrient );
|
|
|
|
|
|
|
|
aLocation = TopLoc_Location( lPos );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::readIGES( Handle( TDocStd_Document )& doc, const char* fname )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
IGESControl_Controller::Init();
|
|
|
|
IGESCAFControl_Reader reader;
|
|
|
|
IFSelect_ReturnStatus stat = reader.ReadFile( fname );
|
|
|
|
|
|
|
|
if( stat != IFSelect_RetDone )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Enable user-defined shape precision
|
|
|
|
if( !Interface_Static::SetIVal( "read.precision.mode", 1 ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Set the shape conversion precision to USER_PREC (default 0.0001 has too many triangles)
|
|
|
|
if( !Interface_Static::SetRVal( "read.precision.val", USER_PREC ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// set other translation options
|
2022-07-06 21:32:52 +00:00
|
|
|
reader.SetColorMode( true ); // use model colors
|
|
|
|
reader.SetNameMode( false ); // don't use IGES label names
|
|
|
|
reader.SetLayerMode( false ); // ignore LAYER data
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-07-06 21:32:52 +00:00
|
|
|
if( !reader.Transfer( doc ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
doc->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are there any shapes to translate?
|
|
|
|
if( reader.NbShapes() < 1 )
|
|
|
|
{
|
|
|
|
doc->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
bool STEP_PCB_MODEL::readSTEP( Handle( TDocStd_Document )& doc, const char* fname )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
STEPCAFControl_Reader reader;
|
|
|
|
IFSelect_ReturnStatus stat = reader.ReadFile( fname );
|
|
|
|
|
|
|
|
if( stat != IFSelect_RetDone )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Enable user-defined shape precision
|
|
|
|
if( !Interface_Static::SetIVal( "read.precision.mode", 1 ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Set the shape conversion precision to USER_PREC (default 0.0001 has too many triangles)
|
|
|
|
if( !Interface_Static::SetRVal( "read.precision.val", USER_PREC ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// set other translation options
|
2022-07-06 21:32:52 +00:00
|
|
|
reader.SetColorMode( true ); // use model colors
|
|
|
|
reader.SetNameMode( false ); // don't use label names
|
|
|
|
reader.SetLayerMode( false ); // ignore LAYER data
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-07-06 21:32:52 +00:00
|
|
|
if( !reader.Transfer( doc ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
doc->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are there any shapes to translate?
|
|
|
|
if( reader.NbRootsForTransfer() < 1 )
|
|
|
|
{
|
|
|
|
doc->Close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-08 22:00:05 +00:00
|
|
|
TDF_Label STEP_PCB_MODEL::transferModel( Handle( TDocStd_Document )& source,
|
|
|
|
Handle( TDocStd_Document )& dest, VECTOR3D aScale )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
// transfer data from Source into a top level component of Dest
|
2019-05-28 15:40:34 +00:00
|
|
|
gp_GTrsf scale_transform;
|
|
|
|
scale_transform.SetVectorialPart( gp_Mat( aScale.x, 0, 0,
|
|
|
|
0, aScale.y, 0,
|
|
|
|
0, 0, aScale.z ) );
|
|
|
|
BRepBuilderAPI_GTransform brep( scale_transform );
|
|
|
|
|
2016-09-02 10:08:40 +00:00
|
|
|
// s_assy = shape tool for the source
|
2022-07-06 21:32:52 +00:00
|
|
|
Handle(XCAFDoc_ShapeTool) s_assy = XCAFDoc_DocumentTool::ShapeTool( source->Main() );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
// retrieve all free shapes within the assembly
|
|
|
|
TDF_LabelSequence frshapes;
|
|
|
|
s_assy->GetFreeShapes( frshapes );
|
|
|
|
|
|
|
|
// d_assy = shape tool for the destination
|
2022-07-06 21:32:52 +00:00
|
|
|
Handle( XCAFDoc_ShapeTool ) d_assy = XCAFDoc_DocumentTool::ShapeTool ( dest->Main() );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
// create a new shape within the destination and set the assembly tool to point to it
|
|
|
|
TDF_Label component = d_assy->NewShape();
|
|
|
|
|
|
|
|
int nshapes = frshapes.Length();
|
|
|
|
int id = 1;
|
|
|
|
Handle( XCAFDoc_ColorTool ) scolor = XCAFDoc_DocumentTool::ColorTool( source->Main() );
|
|
|
|
Handle( XCAFDoc_ColorTool ) dcolor = XCAFDoc_DocumentTool::ColorTool( dest->Main() );
|
|
|
|
TopExp_Explorer dtop;
|
|
|
|
TopExp_Explorer stop;
|
|
|
|
|
|
|
|
while( id <= nshapes )
|
|
|
|
{
|
2022-07-06 21:32:52 +00:00
|
|
|
TopoDS_Shape shape = s_assy->GetShape( frshapes.Value( id ) );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
2022-07-06 21:32:52 +00:00
|
|
|
if( !shape.IsNull() )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
2022-07-19 17:35:31 +00:00
|
|
|
TopoDS_Shape scaled_shape( shape );
|
2019-05-28 15:40:34 +00:00
|
|
|
|
2022-07-19 17:35:31 +00:00
|
|
|
if( aScale.x != 1.0 || aScale.y != 1.0 || aScale.z != 1.0 )
|
2020-02-15 09:58:44 +00:00
|
|
|
{
|
2022-07-19 17:35:31 +00:00
|
|
|
brep.Perform( shape, Standard_False );
|
|
|
|
|
|
|
|
if( brep.IsDone() )
|
|
|
|
{
|
|
|
|
scaled_shape = brep.Shape();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ReportMessage( wxT( " * transfertModel(): failed to scale model\n" ) );
|
2020-02-15 09:58:44 +00:00
|
|
|
|
2022-07-19 17:35:31 +00:00
|
|
|
scaled_shape = shape;
|
|
|
|
}
|
2019-05-28 15:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TDF_Label niulab = d_assy->AddComponent( component, scaled_shape, Standard_False );
|
2016-09-02 10:08:40 +00:00
|
|
|
|
|
|
|
// check for per-surface colors
|
|
|
|
stop.Init( shape, TopAbs_FACE );
|
|
|
|
dtop.Init( d_assy->GetShape( niulab ), TopAbs_FACE );
|
|
|
|
|
|
|
|
while( stop.More() && dtop.More() )
|
|
|
|
{
|
|
|
|
Quantity_Color face_color;
|
|
|
|
|
|
|
|
TDF_Label tl;
|
|
|
|
|
|
|
|
// give priority to the base shape's color
|
|
|
|
if( s_assy->FindShape( stop.Current(), tl ) )
|
|
|
|
{
|
|
|
|
if( scolor->GetColor( tl, XCAFDoc_ColorSurf, face_color )
|
|
|
|
|| scolor->GetColor( tl, XCAFDoc_ColorGen, face_color )
|
|
|
|
|| scolor->GetColor( tl, XCAFDoc_ColorCurv, face_color ) )
|
|
|
|
{
|
|
|
|
dcolor->SetColor( dtop.Current(), face_color, XCAFDoc_ColorSurf );
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 21:32:52 +00:00
|
|
|
else if( scolor->GetColor( stop.Current(), XCAFDoc_ColorSurf, face_color )
|
|
|
|
|| scolor->GetColor( stop.Current(), XCAFDoc_ColorGen, face_color )
|
|
|
|
|| scolor->GetColor( stop.Current(), XCAFDoc_ColorCurv, face_color ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
dcolor->SetColor( dtop.Current(), face_color, XCAFDoc_ColorSurf );
|
|
|
|
}
|
|
|
|
|
|
|
|
stop.Next();
|
|
|
|
dtop.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for per-solid colors
|
|
|
|
stop.Init( shape, TopAbs_SOLID );
|
|
|
|
dtop.Init( d_assy->GetShape( niulab ), TopAbs_SOLID, TopAbs_FACE );
|
|
|
|
|
|
|
|
while( stop.More() && dtop.More() )
|
|
|
|
{
|
|
|
|
Quantity_Color face_color;
|
|
|
|
|
|
|
|
TDF_Label tl;
|
|
|
|
|
|
|
|
// give priority to the base shape's color
|
|
|
|
if( s_assy->FindShape( stop.Current(), tl ) )
|
|
|
|
{
|
|
|
|
if( scolor->GetColor( tl, XCAFDoc_ColorSurf, face_color )
|
|
|
|
|| scolor->GetColor( tl, XCAFDoc_ColorGen, face_color )
|
|
|
|
|| scolor->GetColor( tl, XCAFDoc_ColorCurv, face_color ) )
|
|
|
|
{
|
|
|
|
dcolor->SetColor( dtop.Current(), face_color, XCAFDoc_ColorGen );
|
|
|
|
}
|
|
|
|
}
|
2022-07-06 21:32:52 +00:00
|
|
|
else if( scolor->GetColor( stop.Current(), XCAFDoc_ColorSurf, face_color )
|
|
|
|
|| scolor->GetColor( stop.Current(), XCAFDoc_ColorGen, face_color )
|
|
|
|
|| scolor->GetColor( stop.Current(), XCAFDoc_ColorCurv, face_color ) )
|
2016-09-02 10:08:40 +00:00
|
|
|
{
|
|
|
|
dcolor->SetColor( dtop.Current(), face_color, XCAFDoc_ColorSurf );
|
|
|
|
}
|
|
|
|
|
|
|
|
stop.Next();
|
|
|
|
dtop.Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++id;
|
|
|
|
};
|
|
|
|
|
|
|
|
return component;
|
2022-10-08 22:00:05 +00:00
|
|
|
}
|