2016-08-22 05:50:38 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Cirilo Bernardo <cirilo.bernardo@gmail.com>
|
2023-11-22 15:53:59 +00:00
|
|
|
* Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-08-22 05:50:38 +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
|
2021-10-11 23:25:12 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Some code lifted from FreeCAD, copyright (c) 2018 Zheng, Lei (realthunder) under GPLv2
|
2016-08-22 05:50:38 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2020-08-17 22:48:20 +00:00
|
|
|
#include <wx/filename.h>
|
2021-10-08 02:48:19 +00:00
|
|
|
#include <wx/log.h>
|
2020-08-17 22:48:20 +00:00
|
|
|
#include <wx/stdpaths.h>
|
2017-02-22 09:22:33 +00:00
|
|
|
#include <wx/string.h>
|
2021-03-02 18:34:28 +00:00
|
|
|
#include <wx/utils.h>
|
2017-02-22 09:22:33 +00:00
|
|
|
#include <wx/wfstream.h>
|
2021-02-28 00:41:14 +00:00
|
|
|
#include <wx/zipstrm.h>
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2024-02-29 19:04:39 +00:00
|
|
|
#include <advanced_config.h>
|
2020-08-17 22:48:20 +00:00
|
|
|
#include <decompress.hpp>
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
#include <TDocStd_Document.hxx>
|
|
|
|
#include <TopoDS.hxx>
|
|
|
|
#include <TopoDS_Shape.hxx>
|
|
|
|
#include <Quantity_Color.hxx>
|
|
|
|
#include <XCAFApp_Application.hxx>
|
|
|
|
|
|
|
|
#include <AIS_Shape.hxx>
|
|
|
|
|
|
|
|
#include <IGESControl_Reader.hxx>
|
|
|
|
#include <IGESCAFControl_Reader.hxx>
|
|
|
|
#include <Interface_Static.hxx>
|
|
|
|
|
|
|
|
#include <STEPControl_Reader.hxx>
|
|
|
|
#include <STEPCAFControl_Reader.hxx>
|
|
|
|
|
|
|
|
#include <XCAFDoc_DocumentTool.hxx>
|
|
|
|
#include <XCAFDoc_ColorTool.hxx>
|
|
|
|
#include <XCAFDoc_ShapeTool.hxx>
|
|
|
|
|
|
|
|
#include <BRep_Tool.hxx>
|
|
|
|
#include <BRepMesh_IncrementalMesh.hxx>
|
|
|
|
|
|
|
|
#include <TopoDS.hxx>
|
|
|
|
#include <TopoDS_Shape.hxx>
|
|
|
|
#include <TopoDS_Face.hxx>
|
|
|
|
#include <TopoDS_Compound.hxx>
|
|
|
|
#include <TopExp_Explorer.hxx>
|
|
|
|
|
|
|
|
#include <Quantity_Color.hxx>
|
|
|
|
#include <Poly_Triangulation.hxx>
|
|
|
|
#include <Poly_PolygonOnTriangulation.hxx>
|
|
|
|
#include <Precision.hxx>
|
|
|
|
|
|
|
|
#include <TDF_LabelSequence.hxx>
|
|
|
|
#include <TDF_ChildIterator.hxx>
|
2021-10-11 00:42:58 +00:00
|
|
|
#include <TDF_Tool.hxx>
|
2021-10-11 00:07:44 +00:00
|
|
|
#include <TDataStd_Name.hxx>
|
2022-09-09 01:43:11 +00:00
|
|
|
#include <Standard_Version.hxx>
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
#include "plugins/3dapi/ifsg_all.h"
|
|
|
|
|
2021-08-27 12:34:49 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
// log mask for wxLogTrace
|
2022-02-05 13:25:43 +00:00
|
|
|
#define MASK_OCE wxT( "PLUGIN_OCE" )
|
|
|
|
#define MASK_OCE_EXTRA wxT( "PLUGIN_OCE_EXTRA" )
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
typedef std::map<std::size_t, SGNODE*> COLORMAP;
|
|
|
|
typedef std::map<std::string, SGNODE*> FACEMAP;
|
|
|
|
typedef std::map<std::string, std::vector<SGNODE*>> NODEMAP;
|
|
|
|
typedef std::pair<std::string, std::vector<SGNODE*>> NODEITEM;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
struct DATA;
|
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
bool processLabel( const TDF_Label& aLabel, DATA& aData, SGNODE* aParent,
|
|
|
|
std::vector< SGNODE* >* aItems );
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
bool processFace( const TopoDS_Face& face, DATA& data, SGNODE* parent,
|
2022-09-09 01:43:11 +00:00
|
|
|
std::vector< SGNODE* >* items, Quantity_ColorRGBA* color );
|
2021-07-18 14:06:48 +00:00
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
#if OCC_VERSION_HEX >= 0x070500
|
|
|
|
#define OCC_COLOR_SPACE Quantity_TOC_sRGB
|
|
|
|
#else
|
|
|
|
#define OCC_COLOR_SPACE Quantity_TOC_RGB
|
|
|
|
#endif
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
struct DATA
|
|
|
|
{
|
|
|
|
Handle( TDocStd_Document ) m_doc;
|
|
|
|
Handle( XCAFDoc_ColorTool ) m_color;
|
|
|
|
Handle( XCAFDoc_ShapeTool ) m_assy;
|
|
|
|
SGNODE* scene;
|
|
|
|
SGNODE* defaultColor;
|
|
|
|
Quantity_Color refColor;
|
|
|
|
NODEMAP shapes; // SGNODE lists representing a TopoDS_SOLID / COMPOUND
|
|
|
|
COLORMAP colors; // SGAPPEARANCE nodes
|
|
|
|
FACEMAP faces; // SGSHAPE items representing a TopoDS_FACE
|
|
|
|
bool renderBoth; // set TRUE if we're processing IGES
|
|
|
|
bool hasSolid; // set TRUE if there is no parent SOLID
|
|
|
|
|
|
|
|
DATA()
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
scene = nullptr;
|
|
|
|
defaultColor = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
refColor.SetValues( Quantity_NOC_BLACK );
|
|
|
|
renderBoth = false;
|
|
|
|
hasSolid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~DATA()
|
|
|
|
{
|
|
|
|
// destroy any colors with no parent
|
|
|
|
if( !colors.empty() )
|
|
|
|
{
|
|
|
|
COLORMAP::iterator sC = colors.begin();
|
|
|
|
COLORMAP::iterator eC = colors.end();
|
|
|
|
|
|
|
|
while( sC != eC )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( sC->second ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::DestroyNode( sC->second );
|
|
|
|
|
|
|
|
++sC;
|
|
|
|
}
|
|
|
|
|
|
|
|
colors.clear();
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( defaultColor && nullptr == S3D::GetSGNodeParent( defaultColor ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::DestroyNode(defaultColor);
|
|
|
|
|
|
|
|
// destroy any faces with no parent
|
|
|
|
if( !faces.empty() )
|
|
|
|
{
|
|
|
|
FACEMAP::iterator sF = faces.begin();
|
|
|
|
FACEMAP::iterator eF = faces.end();
|
|
|
|
|
|
|
|
while( sF != eF )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( sF->second ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::DestroyNode( sF->second );
|
|
|
|
|
|
|
|
++sF;
|
|
|
|
}
|
|
|
|
|
|
|
|
faces.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy any shapes with no parent
|
|
|
|
if( !shapes.empty() )
|
|
|
|
{
|
|
|
|
NODEMAP::iterator sS = shapes.begin();
|
|
|
|
NODEMAP::iterator eS = shapes.end();
|
|
|
|
|
|
|
|
while( sS != eS )
|
|
|
|
{
|
|
|
|
std::vector< SGNODE* >::iterator sV = sS->second.begin();
|
|
|
|
std::vector< SGNODE* >::iterator eV = sS->second.end();
|
|
|
|
|
|
|
|
while( sV != eV )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( *sV ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::DestroyNode( *sV );
|
|
|
|
|
|
|
|
++sV;
|
|
|
|
}
|
|
|
|
|
|
|
|
sS->second.clear();
|
|
|
|
++sS;
|
|
|
|
}
|
|
|
|
|
|
|
|
shapes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( scene )
|
|
|
|
S3D::DestroyNode(scene);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find collection of tagged nodes
|
|
|
|
bool GetShape( const std::string& id, std::vector< SGNODE* >*& listPtr )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
listPtr = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
NODEMAP::iterator item;
|
|
|
|
item = shapes.find( id );
|
|
|
|
|
|
|
|
if( item == shapes.end() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
listPtr = &item->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find collection of tagged nodes
|
|
|
|
SGNODE* GetFace( const std::string& id )
|
|
|
|
{
|
|
|
|
FACEMAP::iterator item;
|
|
|
|
item = faces.find( id );
|
|
|
|
|
|
|
|
if( item == faces.end() )
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
return item->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return color if found; if not found, create SGAPPEARANCE
|
2022-09-09 01:43:11 +00:00
|
|
|
SGNODE* GetColor( Quantity_ColorRGBA* colorObj )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == colorObj )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
if( defaultColor )
|
|
|
|
return defaultColor;
|
|
|
|
|
|
|
|
IFSG_APPEARANCE app( true );
|
2020-10-05 10:41:14 +00:00
|
|
|
app.SetShininess( 0.05f );
|
|
|
|
app.SetSpecular( 0.04f, 0.04f, 0.04f );
|
|
|
|
app.SetAmbient( 0.1f, 0.1f, 0.1f );
|
|
|
|
app.SetDiffuse( 0.6f, 0.6f, 0.6f );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
defaultColor = app.GetRawPtr();
|
|
|
|
return defaultColor;
|
|
|
|
}
|
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
Quantity_Color colorRgb = colorObj->GetRGB();
|
|
|
|
|
|
|
|
Standard_Real r, g, b;
|
|
|
|
colorObj->GetRGB().Values( r, g, b, OCC_COLOR_SPACE );
|
|
|
|
|
|
|
|
std::size_t hash = std::hash<double>{}( colorRgb.Distance( refColor ) )
|
|
|
|
^ ( std::hash<float>{}( colorObj->Alpha() ) << 1 );
|
|
|
|
|
|
|
|
std::map<std::size_t, SGNODE*>::iterator item;
|
|
|
|
item = colors.find( hash );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
if( item != colors.end() )
|
|
|
|
return item->second;
|
|
|
|
|
|
|
|
IFSG_APPEARANCE app( true );
|
2020-10-05 10:41:14 +00:00
|
|
|
app.SetShininess( 0.1f );
|
|
|
|
app.SetSpecular( 0.12f, 0.12f, 0.12f );
|
|
|
|
app.SetAmbient( 0.1f, 0.1f, 0.1f );
|
2022-09-09 01:43:11 +00:00
|
|
|
app.SetDiffuse( r, g, b );
|
|
|
|
app.SetTransparency( 1.0f - colorObj->Alpha() );
|
|
|
|
colors.emplace( hash, app.GetRawPtr() );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
return app.GetRawPtr();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
enum FormatType
|
|
|
|
{
|
|
|
|
FMT_NONE = 0,
|
2020-08-17 22:48:20 +00:00
|
|
|
FMT_STEP,
|
|
|
|
FMT_STPZ,
|
|
|
|
FMT_IGES
|
2016-08-22 05:50:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
FormatType fileType( const char* aFileName )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
wxFileName fname( wxString::FromUTF8Unchecked( aFileName ) );
|
2020-12-12 23:51:35 +00:00
|
|
|
wxFFileInputStream ifile( fname.GetFullPath() );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2017-02-22 09:22:33 +00:00
|
|
|
if( !ifile.IsOk() )
|
2016-08-22 05:50:38 +00:00
|
|
|
return FMT_NONE;
|
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
if( fname.GetExt().MakeUpper().EndsWith( wxT( "STPZ" ) )
|
|
|
|
|| fname.GetExt().MakeUpper().EndsWith( wxT( "GZ" ) ) )
|
|
|
|
{
|
2020-08-17 22:48:20 +00:00
|
|
|
return FMT_STPZ;
|
2022-02-05 13:25:43 +00:00
|
|
|
}
|
2020-08-17 22:48:20 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
char iline[82];
|
|
|
|
memset( iline, 0, 82 );
|
2017-02-22 09:22:33 +00:00
|
|
|
ifile.Read( iline, 82 );
|
2016-08-22 05:50:38 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-11 00:42:58 +00:00
|
|
|
/**
|
|
|
|
* Gets the absolute tag string for a given label in the form of ##:##:##:##
|
|
|
|
*
|
|
|
|
* @param aLabel is the label to get the string for
|
|
|
|
* @param aTag is the resulting tag string based by reference
|
|
|
|
*/
|
|
|
|
void getTag( const TDF_Label& aLabel, std::string& aTag )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-10-11 00:42:58 +00:00
|
|
|
std::ostringstream ostr;
|
|
|
|
|
|
|
|
if( aLabel.IsNull() )
|
2021-10-09 02:05:32 +00:00
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Null label passed to getTag" ) );
|
2016-08-22 05:50:38 +00:00
|
|
|
return;
|
2021-10-09 02:05:32 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-10-11 00:42:58 +00:00
|
|
|
TColStd_ListOfInteger tagList;
|
|
|
|
TDF_Tool::TagList( aLabel, tagList );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-10-11 00:42:58 +00:00
|
|
|
for( TColStd_ListOfInteger::Iterator it( tagList ); it.More(); it.Next() )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-10-11 00:42:58 +00:00
|
|
|
ostr << it.Value();
|
|
|
|
ostr << ":";
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
2021-10-11 00:42:58 +00:00
|
|
|
|
|
|
|
aTag = ostr.str();
|
|
|
|
aTag.pop_back(); // kill the last colon
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-11 23:25:12 +00:00
|
|
|
static wxString getLabelName( const TDF_Label& aLabel )
|
|
|
|
{
|
|
|
|
wxString txt;
|
|
|
|
Handle( TDataStd_Name ) name;
|
|
|
|
if( !aLabel.IsNull() && aLabel.FindAttribute( TDataStd_Name::GetID(), name ) )
|
|
|
|
{
|
|
|
|
TCollection_ExtendedString extstr = name->Get();
|
|
|
|
char* str = new char[extstr.LengthOfCString() + 1];
|
|
|
|
extstr.ToUTF8CString( str );
|
|
|
|
|
|
|
|
txt = wxString::FromUTF8( str );
|
|
|
|
delete[] str;
|
|
|
|
txt = txt.Trim();
|
|
|
|
}
|
|
|
|
return txt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a string for a given TopAbs_ShapeEnum element
|
|
|
|
*
|
|
|
|
* @param aShape enum value to convert
|
|
|
|
*/
|
|
|
|
std::string getShapeName( TopAbs_ShapeEnum aShape )
|
|
|
|
{
|
|
|
|
switch( aShape )
|
|
|
|
{
|
|
|
|
case TopAbs_COMPOUND: return "COMPOUND";
|
|
|
|
case TopAbs_COMPSOLID: return "COMPSOLID";
|
|
|
|
case TopAbs_SOLID: return "SOLID";
|
|
|
|
case TopAbs_SHELL: return "SHELL";
|
|
|
|
case TopAbs_FACE: return "FACE";
|
|
|
|
case TopAbs_WIRE: return "WIRE";
|
|
|
|
case TopAbs_EDGE: return "EDGE";
|
|
|
|
case TopAbs_VERTEX: return "VERTEX";
|
|
|
|
case TopAbs_SHAPE: return "SHAPE";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
static int colorFloatToDecimal( float aVal )
|
|
|
|
{
|
|
|
|
return aVal * 255;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline std::ostream& operator<<( std::ostream& aOStream, const Quantity_ColorRGBA& aColor )
|
|
|
|
{
|
|
|
|
Quantity_Color rgb = aColor.GetRGB();
|
|
|
|
|
|
|
|
return aOStream << "rgba(" << colorFloatToDecimal( rgb.Red() ) << ","
|
|
|
|
<< colorFloatToDecimal( rgb.Green() ) << ","
|
|
|
|
<< colorFloatToDecimal( rgb.Blue() ) << ","
|
|
|
|
<< colorFloatToDecimal( aColor.Alpha() )
|
|
|
|
<< ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a string for a given TopAbs_ShapeEnum element
|
|
|
|
*
|
|
|
|
* @param aLabel Label to convert
|
|
|
|
* @param aShapeTool Handle to shape tool being used
|
|
|
|
* @param aColorTool Handle to color tool being used
|
|
|
|
* @param aPregMsg Any prefixed message to insert (used for indentation in dump)
|
|
|
|
*/
|
|
|
|
static void printLabel( TDF_Label aLabel, Handle( XCAFDoc_ShapeTool ) aShapeTool,
|
|
|
|
Handle( XCAFDoc_ColorTool ) aColorTool, const char* aPreMsg = nullptr )
|
|
|
|
{
|
|
|
|
if( aLabel.IsNull() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( !aPreMsg )
|
|
|
|
aPreMsg = "Label: ";
|
|
|
|
|
|
|
|
TCollection_AsciiString entry;
|
|
|
|
TDF_Tool::Entry( aLabel, entry );
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << aPreMsg << entry << ", " << getLabelName( aLabel )
|
|
|
|
<< ( aShapeTool->IsShape( aLabel ) ? ", shape" : "" )
|
|
|
|
<< ( aShapeTool->IsTopLevel( aLabel ) ? ", topLevel" : "" )
|
|
|
|
<< ( aShapeTool->IsFree( aLabel ) ? ", free" : "" )
|
|
|
|
<< ( aShapeTool->IsAssembly( aLabel ) ? ", assembly" : "" )
|
|
|
|
<< ( aShapeTool->IsSimpleShape( aLabel ) ? ", simple" : "" )
|
|
|
|
<< ( aShapeTool->IsCompound( aLabel ) ? ", compound" : "" )
|
|
|
|
<< ( aShapeTool->IsReference( aLabel ) ? ", reference" : "" )
|
|
|
|
<< ( aShapeTool->IsComponent( aLabel ) ? ", component" : "" )
|
|
|
|
<< ( aShapeTool->IsSubShape( aLabel ) ? ", subshape" : "" );
|
|
|
|
|
|
|
|
if( aShapeTool->IsSubShape( aLabel ) )
|
|
|
|
{
|
|
|
|
auto shape = aShapeTool->GetShape( aLabel );
|
|
|
|
if( !shape.IsNull() )
|
|
|
|
ss << ", " << getShapeName( shape.ShapeType() );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( aShapeTool->IsShape( aLabel ) )
|
|
|
|
{
|
|
|
|
Quantity_ColorRGBA c;
|
|
|
|
if( aColorTool->GetColor( aLabel, XCAFDoc_ColorGen, c ) )
|
|
|
|
ss << ", gc: " << c;
|
|
|
|
if( aColorTool->GetColor( aLabel, XCAFDoc_ColorSurf, c ) )
|
|
|
|
ss << ", sc: " << c;
|
|
|
|
if( aColorTool->GetColor( aLabel, XCAFDoc_ColorCurv, c ) )
|
|
|
|
ss << ", cc: " << c;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxLogTrace( MASK_OCE, ss.str().c_str() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dumps a label and the entire tree underneath it
|
|
|
|
*
|
|
|
|
* @param aLabel Label to convert
|
|
|
|
* @param aShapeTool Handle to shape tool being used
|
|
|
|
* @param aColorTool Handle to color tool being used
|
|
|
|
* @param aDepth Indentation level to offset labels (used recursively by dumpLabels)
|
|
|
|
*/
|
|
|
|
static void dumpLabels( TDF_Label aLabel, Handle( XCAFDoc_ShapeTool ) aShapeTool,
|
|
|
|
Handle( XCAFDoc_ColorTool ) aColorTool, int aDepth = 0 )
|
|
|
|
{
|
|
|
|
std::string indent( aDepth * 2, ' ' );
|
|
|
|
printLabel( aLabel, aShapeTool, aColorTool, indent.c_str() );
|
|
|
|
TDF_ChildIterator it;
|
|
|
|
for( it.Initialize( aLabel ); it.More(); it.Next() )
|
|
|
|
dumpLabels( it.Value(), aShapeTool, aColorTool, aDepth + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
bool getColor( DATA& data, TDF_Label label, Quantity_ColorRGBA& color )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
if( data.m_color->GetColor( label, XCAFDoc_ColorGen, color ) )
|
|
|
|
return true;
|
|
|
|
else if( data.m_color->GetColor( label, XCAFDoc_ColorSurf, color ) )
|
|
|
|
return true;
|
|
|
|
else if( data.m_color->GetColor( label, XCAFDoc_ColorCurv, color ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
label = label.Father();
|
|
|
|
|
|
|
|
if( label.IsNull() )
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void addItems( SGNODE* parent, std::vector< SGNODE* >* lp )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == lp )
|
2016-08-22 05:50:38 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector< SGNODE* >::iterator sL = lp->begin();
|
|
|
|
std::vector< SGNODE* >::iterator eL = lp->end();
|
|
|
|
SGNODE* item;
|
|
|
|
|
|
|
|
while( sL != eL )
|
|
|
|
{
|
|
|
|
item = *sL;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( item ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::AddSGNodeChild( parent, item );
|
|
|
|
else
|
|
|
|
S3D::AddSGNodeRef( parent, item );
|
|
|
|
|
|
|
|
++sL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
bool readIGES( Handle( TDocStd_Document ) & m_doc, const char* fname )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
IGESCAFControl_Reader reader;
|
|
|
|
IFSelect_ReturnStatus stat = reader.ReadFile( fname );
|
|
|
|
reader.PrintCheckLoad( Standard_False, IFSelect_ItemsByEntity );
|
|
|
|
|
|
|
|
if( stat != IFSelect_RetDone )
|
|
|
|
return false;
|
|
|
|
|
2018-02-19 21:40:58 +00:00
|
|
|
// Enable file-defined shape precision
|
|
|
|
if( !Interface_Static::SetIVal( "read.precision.mode", 0 ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// set other translation options
|
|
|
|
reader.SetColorMode(true); // use model colors
|
|
|
|
reader.SetNameMode(false); // don't use IGES label names
|
|
|
|
reader.SetLayerMode(false); // ignore LAYER data
|
|
|
|
|
2023-12-06 00:16:43 +00:00
|
|
|
if( !reader.Transfer( m_doc ) )
|
|
|
|
{
|
|
|
|
if( m_doc->CanClose() == CDM_CCS_OK )
|
|
|
|
m_doc->Close();
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
2023-12-06 00:16:43 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
// are there any shapes to translate?
|
|
|
|
if( reader.NbShapes() < 1 )
|
2023-12-06 00:16:43 +00:00
|
|
|
{
|
|
|
|
if( m_doc->CanClose() == CDM_CCS_OK )
|
|
|
|
m_doc->Close();
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
2023-12-06 00:16:43 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool readSTEP( Handle(TDocStd_Document)& m_doc, const char* fname )
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Reading step file %s" ), fname );
|
2021-10-08 02:48:19 +00:00
|
|
|
|
2016-08-22 05:50:38 +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;
|
|
|
|
|
2024-02-29 19:04:39 +00:00
|
|
|
// Set the shape conversion precision (default 0.0001 has too many triangles)
|
|
|
|
if( !Interface_Static::SetRVal( "read.precision.val", ADVANCED_CFG::GetCfg().m_OcePluginLinearDeflection ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// set other translation options
|
2021-07-18 14:06:48 +00:00
|
|
|
reader.SetColorMode( true ); // use model colors
|
|
|
|
reader.SetNameMode( false ); // don't use label names
|
|
|
|
reader.SetLayerMode( false ); // ignore LAYER data
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2023-12-06 00:16:43 +00:00
|
|
|
if( !reader.Transfer( m_doc ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2023-12-06 00:16:43 +00:00
|
|
|
if( m_doc->CanClose() == CDM_CCS_OK )
|
|
|
|
m_doc->Close();
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// are there any shapes to translate?
|
|
|
|
if( reader.NbRootsForTransfer() < 1 )
|
2023-12-06 00:16:43 +00:00
|
|
|
{
|
|
|
|
if( m_doc->CanClose() == CDM_CCS_OK )
|
|
|
|
m_doc->Close();
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return false;
|
2023-12-06 00:16:43 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-17 22:48:20 +00:00
|
|
|
bool readSTEPZ( Handle(TDocStd_Document)& m_doc, const char* aFileName )
|
|
|
|
{
|
|
|
|
wxFileName fname( wxString::FromUTF8Unchecked( aFileName ) );
|
2020-12-12 23:51:35 +00:00
|
|
|
wxFFileInputStream ifile( fname.GetFullPath() );
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
wxFileName outFile( fname );
|
|
|
|
|
|
|
|
outFile.SetPath( wxStandardPaths::Get().GetTempDir() );
|
2022-02-05 13:25:43 +00:00
|
|
|
outFile.SetExt( wxT( "STEP" ) );
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
wxFileOffset size = ifile.GetLength();
|
2021-03-02 18:34:28 +00:00
|
|
|
wxBusyCursor busycursor;
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
if( size == wxInvalidOffset )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
{
|
2021-02-28 00:41:14 +00:00
|
|
|
bool success = false;
|
2020-12-12 23:51:35 +00:00
|
|
|
wxFFileOutputStream ofile( outFile.GetFullPath() );
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
if( !ofile.IsOk() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *buffer = new char[size];
|
|
|
|
|
|
|
|
ifile.Read( buffer, size);
|
2020-08-26 21:18:54 +00:00
|
|
|
std::string expanded;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
expanded = gzip::decompress( buffer, size );
|
2021-02-28 00:41:14 +00:00
|
|
|
success = true;
|
2020-08-26 21:18:54 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
2021-02-28 00:41:14 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
if( expanded.empty() )
|
2020-08-26 21:18:54 +00:00
|
|
|
{
|
2021-02-28 00:41:14 +00:00
|
|
|
ifile.Reset();
|
|
|
|
ifile.SeekI( 0 );
|
|
|
|
wxZipInputStream izipfile( ifile );
|
|
|
|
std::unique_ptr<wxZipEntry> zip_file( izipfile.GetNextEntry() );
|
|
|
|
|
|
|
|
if( zip_file && !zip_file->IsDir() && izipfile.CanRead() )
|
|
|
|
{
|
|
|
|
izipfile.Read( ofile );
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ofile.Write( expanded.data(), expanded.size() );
|
2020-08-26 21:18:54 +00:00
|
|
|
}
|
2020-08-17 22:48:20 +00:00
|
|
|
|
2020-08-19 10:34:30 +00:00
|
|
|
delete[] buffer;
|
2020-08-17 22:48:20 +00:00
|
|
|
ofile.Close();
|
2021-02-28 00:41:14 +00:00
|
|
|
|
2021-03-02 18:34:28 +00:00
|
|
|
if( !success )
|
|
|
|
return false;
|
2020-08-17 22:48:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:07:25 +00:00
|
|
|
bool retval = readSTEP( m_doc, outFile.GetFullPath().mb_str() );
|
2020-08-17 22:48:20 +00:00
|
|
|
|
|
|
|
// Cleanup our temporary file
|
|
|
|
wxRemoveFile( outFile.GetFullPath() );
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
SCENEGRAPH* LoadModel( char const* filename )
|
|
|
|
{
|
|
|
|
DATA data;
|
|
|
|
|
|
|
|
Handle(XCAFApp_Application) m_app = XCAFApp_Application::GetApplication();
|
|
|
|
m_app->NewDocument( "MDTV-XCAF", data.m_doc );
|
|
|
|
FormatType modelFmt = fileType( filename );
|
|
|
|
|
|
|
|
switch( modelFmt )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
case FMT_IGES:
|
|
|
|
data.renderBoth = true;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( !readIGES( data.m_doc, filename ) )
|
|
|
|
return nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
break;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
case FMT_STEP:
|
|
|
|
if( !readSTEP( data.m_doc, filename ) )
|
|
|
|
return nullptr;
|
2020-08-17 22:48:20 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
break;
|
2020-08-17 22:48:20 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
case FMT_STPZ:
|
|
|
|
if( !readSTEPZ( data.m_doc, filename ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
2023-12-06 00:16:43 +00:00
|
|
|
if( m_app->CanClose( data.m_doc ) == CDM_CCS_OK )
|
|
|
|
m_app->Close( data.m_doc );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
|
|
|
break;
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.m_assy = XCAFDoc_DocumentTool::ShapeTool( data.m_doc->Main() );
|
|
|
|
data.m_color = XCAFDoc_DocumentTool::ColorTool( data.m_doc->Main() );
|
|
|
|
|
2021-10-11 23:49:44 +00:00
|
|
|
// Check if the log mask is enabled otherwise the dump routine may be expensive before the wxLog call
|
|
|
|
if( wxLog::IsAllowedTraceMask( MASK_OCE ) )
|
|
|
|
{
|
|
|
|
dumpLabels( data.m_doc->Main(), data.m_assy, data.m_color );
|
|
|
|
}
|
2021-10-11 23:25:12 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
// retrieve all free shapes
|
|
|
|
TDF_LabelSequence frshapes;
|
|
|
|
data.m_assy->GetFreeShapes( frshapes );
|
|
|
|
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
// create the top level SG node
|
|
|
|
IFSG_TRANSFORM topNode( true );
|
|
|
|
data.scene = topNode.GetRawPtr();
|
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
for( Standard_Integer i = 1; i <= frshapes.Length(); i++ )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-10-09 02:05:32 +00:00
|
|
|
const TDF_Label& label = frshapes.Value( i );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
if( data.m_color->IsVisible( label ) )
|
|
|
|
{
|
|
|
|
if( processLabel( label, data, data.scene, nullptr ) )
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
if( !ret )
|
2022-02-06 15:48:04 +00:00
|
|
|
{
|
2023-12-06 00:16:43 +00:00
|
|
|
if( m_app->CanClose( data.m_doc ) == CDM_CCS_OK )
|
|
|
|
m_app->Close( data.m_doc );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
return nullptr;
|
2022-02-06 15:48:04 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
SCENEGRAPH* scene = (SCENEGRAPH*)data.scene;
|
|
|
|
|
|
|
|
// DEBUG: WRITE OUT VRML2 FILE TO CONFIRM STRUCTURE
|
2021-07-18 14:06:48 +00:00
|
|
|
#if ( defined( DEBUG_OCE ) && DEBUG_OCE > 3 )
|
2016-08-22 05:50:38 +00:00
|
|
|
if( data.scene )
|
|
|
|
{
|
|
|
|
wxFileName fn( wxString::FromUTF8Unchecked( filename ) );
|
|
|
|
wxString output;
|
|
|
|
|
|
|
|
if( FMT_STEP == modelFmt )
|
|
|
|
output = wxT( "_step-" );
|
|
|
|
else
|
|
|
|
output = wxT( "_iges-" );
|
|
|
|
|
|
|
|
output.append( fn.GetName() );
|
2021-07-18 14:06:48 +00:00
|
|
|
output.append( wxT( ".wrl" ) );
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::WriteVRML( output.ToUTF8(), true, data.scene, true, true );
|
|
|
|
}
|
2021-07-18 14:06:48 +00:00
|
|
|
#endif
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
// set to NULL to prevent automatic destruction of the scene data
|
2021-07-18 14:06:48 +00:00
|
|
|
data.scene = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2023-12-06 00:16:43 +00:00
|
|
|
if( m_app->CanClose( data.m_doc ) == CDM_CCS_OK )
|
|
|
|
m_app->Close( data.m_doc );
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool processShell( const TopoDS_Shape& shape, DATA& data, SGNODE* parent,
|
2022-09-09 01:43:11 +00:00
|
|
|
std::vector<SGNODE*>* items, Quantity_ColorRGBA* color )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
TopoDS_Iterator it;
|
|
|
|
bool ret = false;
|
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Processing shell" ) );
|
2016-08-22 05:50:38 +00:00
|
|
|
for( it.Initialize( shape, false, false ); it.More(); it.Next() )
|
|
|
|
{
|
|
|
|
const TopoDS_Face& face = TopoDS::Face( it.Value() );
|
|
|
|
|
|
|
|
if( processFace( face, data, parent, items, color ) )
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-15 00:43:31 +00:00
|
|
|
bool processSolidOrShell( const TopoDS_Shape& shape, DATA& data, SGNODE* parent,
|
2021-07-18 14:06:48 +00:00
|
|
|
std::vector< SGNODE* >* items )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2020-10-13 20:47:18 +00:00
|
|
|
TDF_Label label;
|
2016-08-22 05:50:38 +00:00
|
|
|
std::string partID;
|
2022-09-09 01:43:11 +00:00
|
|
|
Quantity_ColorRGBA col;
|
|
|
|
Quantity_ColorRGBA* lcolor = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2023-11-22 15:53:59 +00:00
|
|
|
data.hasSolid = shape.ShapeType() == TopAbs_SOLID;
|
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Processing solid" ) );
|
2021-10-08 02:48:19 +00:00
|
|
|
|
2020-10-21 22:38:48 +00:00
|
|
|
// Search the whole model first to make sure something exists (may or may not have color)
|
|
|
|
if( !data.m_assy->Search( shape, label ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
static int i = 0;
|
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "KMISC_" << i++;
|
|
|
|
partID = ostr.str();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-21 22:38:48 +00:00
|
|
|
bool found_color = false;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
if( getColor( data, label, col ) )
|
2020-10-21 22:38:48 +00:00
|
|
|
{
|
|
|
|
found_color = true;
|
2016-08-22 05:50:38 +00:00
|
|
|
lcolor = &col;
|
2020-10-21 22:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the top-level label doesn't have the color information, search components
|
|
|
|
if( !found_color )
|
|
|
|
{
|
|
|
|
if( data.m_assy->Search( shape, label, Standard_False, Standard_True, Standard_True ) &&
|
2021-07-18 14:06:48 +00:00
|
|
|
getColor( data, label, col ) )
|
2020-10-21 22:38:48 +00:00
|
|
|
{
|
|
|
|
found_color = true;
|
|
|
|
lcolor = &col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the components do not have color information, search all components without location
|
|
|
|
if( !found_color )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( data.m_assy->Search( shape, label, Standard_False, Standard_False,
|
|
|
|
Standard_True ) &&
|
|
|
|
getColor( data, label, col ) )
|
2020-10-21 22:38:48 +00:00
|
|
|
{
|
|
|
|
found_color = true;
|
|
|
|
lcolor = &col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
// Our last chance to find the color looks for color as a subshape of top-level simple
|
|
|
|
// shapes.
|
2020-10-21 22:38:48 +00:00
|
|
|
if( !found_color )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( data.m_assy->Search( shape, label, Standard_False, Standard_False,
|
|
|
|
Standard_False ) &&
|
|
|
|
getColor( data, label, col ) )
|
2020-10-21 22:38:48 +00:00
|
|
|
{
|
|
|
|
found_color = true;
|
|
|
|
lcolor = &col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getTag( label, partID );
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TopoDS_Iterator it;
|
|
|
|
IFSG_TRANSFORM childNode( parent );
|
|
|
|
SGNODE* pptr = childNode.GetRawPtr();
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
std::vector< SGNODE* >* component = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
if( !partID.empty() )
|
|
|
|
data.GetShape( partID, component );
|
|
|
|
|
|
|
|
if( component )
|
|
|
|
{
|
|
|
|
addItems( pptr, component );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != items )
|
2016-08-22 05:50:38 +00:00
|
|
|
items->push_back( pptr );
|
|
|
|
}
|
|
|
|
|
2023-02-15 00:43:31 +00:00
|
|
|
// instantiate resulting object
|
|
|
|
std::vector<SGNODE*> itemList;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2023-02-15 00:43:31 +00:00
|
|
|
TopAbs_ShapeEnum stype = shape.ShapeType();
|
|
|
|
if( stype == TopAbs_SHELL )
|
|
|
|
{
|
|
|
|
if( processShell( shape, data, pptr, &itemList, lcolor ) )
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
|
2023-02-15 00:43:31 +00:00
|
|
|
for( it.Initialize( shape, false, false ); it.More(); it.Next() )
|
2021-10-09 02:05:32 +00:00
|
|
|
{
|
2023-02-15 00:43:31 +00:00
|
|
|
const TopoDS_Shape& subShape = it.Value();
|
|
|
|
|
|
|
|
if( subShape.ShapeType() == TopAbs_SHELL )
|
|
|
|
{
|
|
|
|
if( processShell( subShape, data, pptr, &itemList, lcolor ) )
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( MASK_OCE, wxT( "Unsupported subshape in solid" ) );
|
|
|
|
}
|
2021-10-09 02:05:32 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !ret )
|
|
|
|
childNode.Destroy();
|
2021-07-18 14:06:48 +00:00
|
|
|
else if( nullptr != items )
|
2016-08-22 05:50:38 +00:00
|
|
|
items->push_back( pptr );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
bool processLabel( const TDF_Label& aLabel, DATA& aData, SGNODE* aParent,
|
|
|
|
std::vector<SGNODE*>* aItems )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-11-06 04:55:31 +00:00
|
|
|
std::string labelTag;
|
|
|
|
|
|
|
|
if( wxLog::IsAllowedTraceMask( MASK_OCE ) )
|
|
|
|
{
|
Fix various typos
Found via `codespell -q 3 -S *.po,./thirdparty,./Documentation/changelogs -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,ba,busses,dout,einstance,leaded,modul,ontext,ot,overide,serie,te,,tesselate,tesselator,tht`
2022-07-03 04:20:05 +00:00
|
|
|
// can be expensive, guard it if we aren't logging
|
2021-11-06 04:55:31 +00:00
|
|
|
getTag( aLabel, labelTag );
|
|
|
|
}
|
|
|
|
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Processing label %s" ), labelTag );
|
2021-10-09 02:05:32 +00:00
|
|
|
|
|
|
|
TopoDS_Shape originalShape;
|
|
|
|
TDF_Label shapeLabel = aLabel;
|
|
|
|
|
|
|
|
if( !aData.m_assy->GetShape( shapeLabel, originalShape ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TopoDS_Shape shape = originalShape;
|
|
|
|
|
|
|
|
if( aData.m_assy->IsReference( aLabel ) )
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Label %s is ref, trying to pull up referred label" ),
|
|
|
|
labelTag );
|
2021-10-09 02:05:32 +00:00
|
|
|
|
|
|
|
if( !aData.m_assy->GetReferredShape( aLabel, shapeLabel ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
labelTag = static_cast<int>( shapeLabel.Tag() );
|
2022-02-05 13:25:43 +00:00
|
|
|
// wxLogTrace( MASK_OCE, wxT( "Label %s referred" ), labelTag );
|
2021-10-09 02:05:32 +00:00
|
|
|
|
|
|
|
if( !aData.m_assy->GetShape( shapeLabel, shape ) )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
// Now let's see if the original label has a location
|
|
|
|
// Labels can be used to place copies of other labels at a specific location
|
|
|
|
IFSG_TRANSFORM childNode( aParent );
|
|
|
|
SGNODE* pptr = childNode.GetRawPtr();
|
|
|
|
const TopLoc_Location& loc = originalShape.Location();
|
2021-10-08 02:48:19 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
if( !loc.IsIdentity() )
|
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Label %d has location" ), static_cast<int>( aLabel.Tag() ) );
|
2016-08-22 05:50:38 +00:00
|
|
|
gp_Trsf T = loc.Transformation();
|
2021-10-09 02:05:32 +00:00
|
|
|
gp_XYZ coord = T.TranslationPart();
|
2016-08-22 05:50:38 +00:00
|
|
|
childNode.SetTranslation( SGPOINT( coord.X(), coord.Y(), coord.Z() ) );
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Translation %f, %f, %f" ), coord.X(), coord.Y(), coord.Z() );
|
2021-10-09 02:05:32 +00:00
|
|
|
gp_XYZ axis;
|
2016-08-22 05:50:38 +00:00
|
|
|
Standard_Real angle;
|
|
|
|
|
|
|
|
if( T.GetRotation( axis, angle ) )
|
2021-10-08 02:48:19 +00:00
|
|
|
{
|
2016-08-22 05:50:38 +00:00
|
|
|
childNode.SetRotation( SGVECTOR( axis.X(), axis.Y(), axis.Z() ), angle );
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Rotation %f, %f, %f, angle %f" ),
|
|
|
|
axis.X(), axis.Y(), axis.Z(), angle );
|
2021-10-08 02:48:19 +00:00
|
|
|
}
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TopAbs_ShapeEnum stype = shape.ShapeType();
|
2021-10-09 02:05:32 +00:00
|
|
|
bool ret = false;
|
|
|
|
aData.hasSolid = false;
|
2021-10-08 02:48:19 +00:00
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
switch( stype )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
case TopAbs_COMPOUND:
|
2021-11-06 04:55:31 +00:00
|
|
|
{
|
|
|
|
// assemblies will report a shape type of compound which isn't what we are after
|
|
|
|
// we will still process the children of assemblies but they should just be label references to the actual shapes
|
|
|
|
if( !aData.m_assy->IsAssembly( shapeLabel ) )
|
|
|
|
{
|
|
|
|
TopExp_Explorer xp;
|
|
|
|
|
|
|
|
for( xp.Init( shape, TopAbs_SOLID ); xp.More(); xp.Next() )
|
|
|
|
{
|
2023-02-15 00:43:31 +00:00
|
|
|
processSolidOrShell( xp.Current(), aData, pptr, aItems );
|
2021-11-06 04:55:31 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:21:20 +00:00
|
|
|
// Get all shells, avoid those that may be attached to solids
|
2021-11-06 04:55:31 +00:00
|
|
|
for( xp.Init( shape, TopAbs_SHELL, TopAbs_SOLID ); xp.More(); xp.Next() )
|
|
|
|
{
|
2023-02-15 00:43:31 +00:00
|
|
|
processSolidOrShell( xp.Current(), aData, pptr, aItems );
|
2021-11-06 04:55:31 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
2023-01-30 03:35:07 +00:00
|
|
|
|
2023-02-14 03:21:20 +00:00
|
|
|
// Get all faces, avoid those that may be attached to shells
|
|
|
|
for( xp.Init( shape, TopAbs_FACE, TopAbs_SHELL ); xp.More(); xp.Next() )
|
2023-01-30 03:35:07 +00:00
|
|
|
{
|
|
|
|
const TopoDS_Face& face = TopoDS::Face( xp.Current() );
|
|
|
|
processFace( face, aData, pptr, aItems, nullptr );
|
|
|
|
ret = true;
|
|
|
|
}
|
2021-11-06 04:55:31 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 14:06:48 +00:00
|
|
|
break;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
case TopAbs_SOLID:
|
2023-02-15 00:43:31 +00:00
|
|
|
if( processSolidOrShell( shape, aData, pptr, aItems ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
ret = true;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
break;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
case TopAbs_SHELL:
|
2023-02-15 00:43:31 +00:00
|
|
|
if( processSolidOrShell( shape, aData, pptr, aItems ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TopAbs_FACE:
|
2021-10-09 02:05:32 +00:00
|
|
|
if( processFace( TopoDS::Face( shape ), aData, pptr, aItems, nullptr ) )
|
2021-07-18 14:06:48 +00:00
|
|
|
ret = true;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
if( nullptr != aItems )
|
|
|
|
aItems->push_back( pptr );
|
|
|
|
|
2022-03-10 02:30:58 +00:00
|
|
|
if( !aData.m_assy->IsSimpleShape( shapeLabel ) && shapeLabel.HasChild() )
|
2021-10-09 02:05:32 +00:00
|
|
|
{
|
2022-02-05 13:25:43 +00:00
|
|
|
wxLogTrace( MASK_OCE, wxT( "Label %s has children" ), labelTag );
|
2021-10-09 02:05:32 +00:00
|
|
|
TDF_ChildIterator it;
|
2022-02-05 13:25:43 +00:00
|
|
|
|
2021-10-09 02:05:32 +00:00
|
|
|
for( it.Initialize( shapeLabel ); it.More(); it.Next() )
|
|
|
|
{
|
|
|
|
if( processLabel( it.Value(), aData, pptr, aItems ) )
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 05:50:38 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
bool processFace( const TopoDS_Face& face, DATA& data, SGNODE* parent, std::vector<SGNODE*>* items,
|
|
|
|
Quantity_ColorRGBA* color )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
if( Standard_True == face.IsNull() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool reverse = ( face.Orientation() == TopAbs_REVERSED );
|
2021-07-18 14:06:48 +00:00
|
|
|
SGNODE* ashape = nullptr;
|
2016-08-22 05:50:38 +00:00
|
|
|
std::string partID;
|
|
|
|
TDF_Label label;
|
|
|
|
|
|
|
|
bool useBothSides = false;
|
|
|
|
|
|
|
|
// for IGES renderBoth = TRUE; for STEP if a shell or face is not a descendant
|
|
|
|
// of a SOLID then hasSolid = false and we must render both sides
|
|
|
|
if( data.renderBoth || !data.hasSolid )
|
|
|
|
useBothSides = true;
|
|
|
|
|
|
|
|
if( data.m_assy->FindShape( face, label, Standard_False ) )
|
|
|
|
getTag( label, partID );
|
|
|
|
|
|
|
|
if( !partID.empty() )
|
|
|
|
ashape = data.GetFace( partID );
|
|
|
|
|
|
|
|
if( ashape )
|
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( ashape ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::AddSGNodeChild( parent, ashape );
|
|
|
|
else
|
|
|
|
S3D::AddSGNodeRef( parent, ashape );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != items )
|
2016-08-22 05:50:38 +00:00
|
|
|
items->push_back( ashape );
|
|
|
|
|
|
|
|
if( useBothSides )
|
|
|
|
{
|
|
|
|
std::string id2 = partID;
|
|
|
|
id2.append( "b" );
|
|
|
|
SGNODE* shapeB = data.GetFace( id2 );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( shapeB ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::AddSGNodeChild( parent, shapeB );
|
|
|
|
else
|
|
|
|
S3D::AddSGNodeRef( parent, shapeB );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr != items )
|
2016-08-22 05:50:38 +00:00
|
|
|
items->push_back( shapeB );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TopLoc_Location loc;
|
|
|
|
Standard_Boolean isTessellate (Standard_False);
|
2021-07-18 14:06:48 +00:00
|
|
|
Handle( Poly_Triangulation ) triangulation = BRep_Tool::Triangulation( face, loc );
|
2024-02-29 19:04:39 +00:00
|
|
|
const double linDeflection = ADVANCED_CFG::GetCfg().m_OcePluginLinearDeflection;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
2024-02-29 19:04:39 +00:00
|
|
|
if( triangulation.IsNull() || triangulation->Deflection() > linDeflection + Precision::Confusion() )
|
2016-08-22 05:50:38 +00:00
|
|
|
isTessellate = Standard_True;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( isTessellate )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2024-02-29 19:04:39 +00:00
|
|
|
BRepMesh_IncrementalMesh IM(face, linDeflection, Standard_False,
|
|
|
|
glm::radians(ADVANCED_CFG::GetCfg().m_OcePluginAngularDeflection) );
|
2016-08-22 05:50:38 +00:00
|
|
|
triangulation = BRep_Tool::Triangulation( face, loc );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( triangulation.IsNull() == Standard_True )
|
|
|
|
return false;
|
|
|
|
|
2022-09-09 01:43:11 +00:00
|
|
|
Quantity_ColorRGBA lcolor;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
// check for a face color; this has precedence over SOLID colors
|
2023-02-15 00:43:31 +00:00
|
|
|
if( data.m_color->GetColor( face, XCAFDoc_ColorGen, lcolor )
|
|
|
|
|| data.m_color->GetColor( face, XCAFDoc_ColorCurv, lcolor )
|
|
|
|
|| data.m_color->GetColor( face, XCAFDoc_ColorSurf, lcolor ) )
|
|
|
|
color = &lcolor;
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
SGNODE* ocolor = data.GetColor( color );
|
|
|
|
|
|
|
|
// create a SHAPE and attach the color and data,
|
|
|
|
// then attach the shape to the parent and return TRUE
|
|
|
|
IFSG_SHAPE vshape( true );
|
|
|
|
IFSG_FACESET vface( vshape );
|
|
|
|
IFSG_COORDS vcoords( vface );
|
|
|
|
IFSG_COORDINDEX coordIdx( vface );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
if( nullptr == S3D::GetSGNodeParent( ocolor ) )
|
2016-08-22 05:50:38 +00:00
|
|
|
S3D::AddSGNodeChild( vshape.GetRawPtr(), ocolor );
|
|
|
|
else
|
|
|
|
S3D::AddSGNodeRef( vshape.GetRawPtr(), ocolor );
|
|
|
|
|
|
|
|
std::vector< SGPOINT > vertices;
|
|
|
|
std::vector< int > indices;
|
|
|
|
std::vector< int > indices2;
|
|
|
|
gp_Trsf tx;
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( int i = 1; i <= triangulation->NbNodes(); i++ )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
2021-11-16 17:08:17 +00:00
|
|
|
gp_XYZ v( triangulation->Node(i).Coord() );
|
2019-12-05 15:41:21 +00:00
|
|
|
vertices.emplace_back( v.X(), v.Y(), v.Z() );
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
for( int i = 1; i <= triangulation->NbTriangles(); i++ )
|
2016-08-22 05:50:38 +00:00
|
|
|
{
|
|
|
|
int a, b, c;
|
2021-11-16 17:08:17 +00:00
|
|
|
triangulation->Triangle(i).Get(a, b, c);
|
2016-08-22 05:50:38 +00:00
|
|
|
a--;
|
|
|
|
|
|
|
|
if( reverse )
|
|
|
|
{
|
|
|
|
int tmp = b - 1;
|
|
|
|
b = c - 1;
|
|
|
|
c = tmp;
|
2021-07-18 14:06:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-22 05:50:38 +00:00
|
|
|
b--;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
|
|
|
|
indices.push_back( a );
|
|
|
|
indices.push_back( b );
|
|
|
|
indices.push_back( c );
|
|
|
|
|
|
|
|
if( useBothSides )
|
|
|
|
{
|
|
|
|
indices2.push_back( b );
|
|
|
|
indices2.push_back( a );
|
|
|
|
indices2.push_back( c );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vcoords.SetCoordsList( vertices.size(), &vertices[0] );
|
|
|
|
coordIdx.SetIndices( indices.size(), &indices[0] );
|
2021-07-18 14:06:48 +00:00
|
|
|
vface.CalcNormals( nullptr );
|
2016-08-22 05:50:38 +00:00
|
|
|
vshape.SetParent( parent );
|
|
|
|
|
|
|
|
if( !partID.empty() )
|
2022-02-06 00:53:31 +00:00
|
|
|
data.faces.emplace( partID, vshape.GetRawPtr() );
|
2016-08-22 05:50:38 +00:00
|
|
|
|
|
|
|
// The outer surface of an IGES model is indeterminate so
|
|
|
|
// we must render both sides of a surface.
|
|
|
|
if( useBothSides )
|
|
|
|
{
|
|
|
|
std::string id2 = partID;
|
|
|
|
id2.append( "b" );
|
|
|
|
IFSG_SHAPE vshape2( true );
|
|
|
|
IFSG_FACESET vface2( vshape2 );
|
|
|
|
IFSG_COORDS vcoords2( vface2 );
|
|
|
|
IFSG_COORDINDEX coordIdx2( vface2 );
|
|
|
|
S3D::AddSGNodeRef( vshape2.GetRawPtr(), ocolor );
|
|
|
|
|
|
|
|
vcoords2.SetCoordsList( vertices.size(), &vertices[0] );
|
|
|
|
coordIdx2.SetIndices( indices2.size(), &indices2[0] );
|
2021-07-18 14:06:48 +00:00
|
|
|
vface2.CalcNormals( nullptr );
|
2016-08-22 05:50:38 +00:00
|
|
|
vshape2.SetParent( parent );
|
|
|
|
|
|
|
|
if( !partID.empty() )
|
2022-02-06 00:53:31 +00:00
|
|
|
data.faces.emplace( id2, vshape2.GetRawPtr() );
|
2016-08-22 05:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|