/
spivag
/
command_occ
Обзор
Документация
Войти
/
spivag
/
command_occ
Код
Запросы
1
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
initial
commands/builder.cpp
310 строк
11 KB
Степанов Никита
Запрос на слияние 'topology_iterators' (
#3
) из topology_iterators в initial
27 апр 2026, 21:55
Верифицирован
27 апр 2026, 21:55
c047a95
Код
Авторство
О чём код?
#include "builder.h" #include <utils.h> #include <BRepPrimAPI_MakeBox.hxx> #include <BRepPrimAPI_MakeCylinder.hxx> #include <BRepPrimAPI_MakeCone.hxx> #include <BRepPrimAPI_MakeTorus.hxx> #include <BRepPrimAPI_MakePrism.hxx> #include <BRepPrimAPI_MakeRevol.hxx> #include <BRepFilletAPI_MakeFillet.hxx> #include <BRepFilletAPI_MakeChamfer.hxx> #include <BRepAlgoAPI_Fuse.hxx> #include <BRepAlgoAPI_Cut.hxx> #include <BRepAlgoAPI_Common.hxx> #include <BRepBuilderAPI_Transform.hxx> //======================================================================================= // Реализации OCC builder-ов //======================================================================================= void OCCMakeBox::CreateCorner( R3::Placement const& placement, double dx, double dy, double dz ) { BRepPrimAPI_MakeBox maker( MakeOccAx2( placement ), dx, dy, dz ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } void OCCMakeBox::CreateCentered( R3::Placement const& placement, double dx, double dy, double dz ) { gp_Vec ox = MakeOccVec( placement.m_ox ); gp_Vec oy = MakeOccVec( placement.m_oy ); gp_Vec oz = ox.Crossed( oy ); gp_Pnt corner = MakeOccPnt( placement.m_origin ) .Translated( ox * ( -dx * 0.5 ) + oy * ( -dy * 0.5 ) + oz * ( -dz * 0.5 ) ); gp_Ax2 ax2{ corner, gp_Dir( oz ), gp_Dir( ox ) }; BRepPrimAPI_MakeBox maker( ax2, dx, dy, dz ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } void OCCMakeBox::CreateBottomCenter( R3::Placement const& placement, double dx, double dy, double dz ) { gp_Vec ox = MakeOccVec( placement.m_ox ); gp_Vec oy = MakeOccVec( placement.m_oy ); gp_Vec oz = ox.Crossed( oy ); gp_Pnt corner = MakeOccPnt( placement.m_origin ) .Translated( ox * ( -dx * 0.5 ) + oy * ( -dy * 0.5 ) ); gp_Ax2 ax2{ corner, gp_Dir( oz ), gp_Dir( ox ) }; BRepPrimAPI_MakeBox maker( ax2, dx, dy, dz ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } void OCCMakeBox::CreateFromCornerPoint( R3::Point const& corner, double dx, double dy, double dz ) { gp_Ax2 ax2{ MakeOccPnt( corner ), gp_Dir( 0, 0, 1 ), gp_Dir( 1, 0, 0 ) }; BRepPrimAPI_MakeBox maker( ax2, dx, dy, dz ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeCylinder::Create( R3::Placement const& placement, double radius, double height ) { BRepPrimAPI_MakeCylinder maker( MakeOccAx2( placement ), radius, height ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } void OCCMakeCylinder::CreateFlipped( R3::Placement const& placement, double radius, double height ) { gp_Vec ox = MakeOccVec( placement.m_ox ); gp_Vec oy = MakeOccVec( placement.m_oy ); gp_Vec oz = ox.Crossed( oy ); gp_Ax2 flippedAx2{ MakeOccPnt( placement.m_origin ), gp_Dir( oz.Reversed() ), gp_Dir( ox ) }; BRepPrimAPI_MakeCylinder maker( flippedAx2, radius, height ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeCone::Create( R3::Placement const& placement, double r1, double r2, double h ) { BRepPrimAPI_MakeCone maker( MakeOccAx2( placement ), r1, r2, h ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeTorus::Create( R3::Placement const& placement, double majorRadius, double minorRadius ) { BRepPrimAPI_MakeTorus maker( MakeOccAx2( placement ), majorRadius, minorRadius ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakePrism::Create( std::shared_ptr< IShape > const& profile, R3::Vector const& direction, double height, std::optional< double > angle ) { auto occProfile = std::dynamic_pointer_cast< OCCShape >( profile ); if ( !occProfile ) return; gp_Vec dir = MakeOccVec( direction ); if ( angle.has_value() ) { BRepPrimAPI_MakePrism maker( occProfile->GetOccShape(), dir, height, angle.value() ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); } else { BRepPrimAPI_MakePrism maker( occProfile->GetOccShape(), dir, height ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); } m_isDone = m_shape && !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeRevol::Create( std::shared_ptr< IShape > const& profile, R3::Axis const& axis, double angle ) { auto occProfile = std::dynamic_pointer_cast< OCCShape >( profile ); if ( !occProfile ) return; gp_Ax1 occAxis{ MakeOccPnt( axis.m_origin ), gp_Dir( MakeOccVec( axis.m_direction ) ) }; BRepPrimAPI_MakeRevol maker( occProfile->GetOccShape(), occAxis, angle ); m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeFillet::AddEdge( std::shared_ptr< IEdge > const& edge, double radius ) { m_edges.push_back( { edge, radius, radius, true } ); } void OCCMakeFillet::AddEdge( std::shared_ptr< IEdge > const& edge, double radius1, double radius2 ) { m_edges.push_back( { edge, radius1, radius2, false } ); } void OCCMakeFillet::Create( std::shared_ptr< IShape > const& shape ) { auto occShape = std::dynamic_pointer_cast< OCCShape >( shape ); if ( !occShape ) return; BRepFilletAPI_MakeFillet maker( occShape->GetOccShape() ); for ( const auto& ep : m_edges ) { auto occEdge = std::dynamic_pointer_cast< OCCEdge >( ep.m_edge ); if ( !occEdge ) continue; if ( ep.m_isSymmetric ) maker.Add( ep.m_radius1, occEdge->GetOccEdge() ); else maker.Add( ep.m_radius1, ep.m_radius2, occEdge->GetOccEdge() ); } maker.Build(); if ( maker.IsDone() ) m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = m_shape && !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeChamfer::AddEdge( std::shared_ptr< IEdge > const& edge, std::shared_ptr< IFace > const& face, double distance ) { m_edges.push_back( { edge, face, distance, distance, true } ); } void OCCMakeChamfer::AddEdge( std::shared_ptr< IEdge > const& edge, std::shared_ptr< IFace > const& face, double distance1, double distance2 ) { m_edges.push_back( { edge, face, distance1, distance2, false } ); } void OCCMakeChamfer::Create( std::shared_ptr< IShape > const& shape ) { auto occShape = std::dynamic_pointer_cast< OCCShape >( shape ); if ( !occShape ) return; BRepFilletAPI_MakeChamfer maker( occShape->GetOccShape() ); for ( const auto& ep : m_edges ) { auto occEdge = std::dynamic_pointer_cast< OCCEdge >( ep.m_edge ); auto occFace = std::dynamic_pointer_cast< OCCFace >( ep.m_face ); if ( !occEdge || !occFace ) continue; if ( ep.m_isSymmetric ) maker.Add( ep.m_distance1, occEdge->GetOccEdge()); else maker.Add( ep.m_distance1, ep.m_distance2, occEdge->GetOccEdge(), occFace->GetOccFace() ); } maker.Build(); if ( maker.IsDone() ) m_shape = std::make_shared<OCCShape>( maker.Shape() ); m_isDone = m_shape && !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeBooleanOp::Create( std::shared_ptr< IShape > const& s1, std::shared_ptr< IShape > const& s2, Operation op ) { auto occ1 = std::dynamic_pointer_cast< OCCShape >( s1 ); auto occ2 = std::dynamic_pointer_cast< OCCShape >( s2 ); if ( !occ1 || !occ2 ) return; switch ( op ) { case Operation::unite: { BRepAlgoAPI_Fuse fuse( occ1->GetOccShape(), occ2->GetOccShape() ); m_shape = std::make_shared<OCCShape>( fuse.Shape() ); break; } case Operation::intersect: { BRepAlgoAPI_Common common( occ1->GetOccShape(), occ2->GetOccShape() ); m_shape = std::make_shared<OCCShape>( common.Shape() ); break; } case Operation::difference: { BRepAlgoAPI_Cut cut( occ1->GetOccShape(), occ2->GetOccShape() ); m_shape = std::make_shared<OCCShape>( cut.Shape() ); break; } } m_isDone = m_shape && !m_shape->IsNull(); } //--------------------------------------------------------------------------------------- void OCCMakeTransform::Create( std::shared_ptr< IShape > const& shape, R3::Placement const& placement ) { auto occShape = std::dynamic_pointer_cast< OCCShape >( shape ); if ( !occShape ) return; BRepBuilderAPI_Transform builder( occShape->GetOccShape(), MakeOccTrsf( placement ), true ); m_shape = std::make_shared<OCCShape>( builder.Shape() ); m_isDone = m_shape && !m_shape->IsNull(); } void OCCMakeTransform::Create( std::shared_ptr< IShape > const& shape, R3::Transform const& transform ) { auto occShape = std::dynamic_pointer_cast< OCCShape >( shape ); if ( !occShape ) return; BRepBuilderAPI_Transform builder( occShape->GetOccShape(), MakeOccTrsf( transform ), true ); m_shape = std::make_shared<OCCShape>( builder.Shape() ); m_isDone = m_shape && !m_shape->IsNull(); } //======================================================================================= // OCCBuilderFactory //======================================================================================= std::shared_ptr< IMakeBox > OCCBuilderFactory::CreateBoxBuilder() { return std::make_shared< OCCMakeBox >(); } std::shared_ptr< IMakeCylinder > OCCBuilderFactory::CreateCylinderBuilder() { return std::make_shared< OCCMakeCylinder >(); } std::shared_ptr< IMakeCone > OCCBuilderFactory::CreateConeBuilder() { return std::make_shared< OCCMakeCone >(); } std::shared_ptr< IMakeTorus > OCCBuilderFactory::CreateTorusBuilder() { return std::make_shared< OCCMakeTorus >(); } std::shared_ptr< IMakePrism > OCCBuilderFactory::CreatePrismBuilder() { return std::make_shared< OCCMakePrism >(); } std::shared_ptr< IMakeRevol > OCCBuilderFactory::CreateRevolBuilder() { return std::make_shared< OCCMakeRevol >(); } std::shared_ptr< IMakeFillet > OCCBuilderFactory::CreateFilletBuilder() { return std::make_shared< OCCMakeFillet >(); } std::shared_ptr< IMakeChamfer > OCCBuilderFactory::CreateChamferBuilder() { return std::make_shared< OCCMakeChamfer >(); } std::shared_ptr< IMakeBooleanOp > OCCBuilderFactory::CreateBooleanOpBuilder() { return std::make_shared< OCCMakeBooleanOp >(); } std::shared_ptr< IMakeTransform > OCCBuilderFactory::CreateTransformBuilder() { return std::make_shared< OCCMakeTransform >(); }