Geom2d_AxisPlacement
教学笔记
一、类概述
Geom2d_AxisPlacement
表示二维几何空间中的一个坐标轴 (轴系),由两部分组成:
gp_Pnt2d
:原点(Location)gp_Dir2d
:单位方向向量(Direction)
它是 Geom2d_Geometry
的派生类,可用于表示局部坐标系、图形基准轴、草图中对齐约束等。
二、构造方法
构造方式 说明 Geom2d_AxisPlacement(gp_Pnt2d P, gp_Dir2d V)
使用原点和单位方向构造坐标轴 Geom2d_AxisPlacement(gp_Ax2d A)
从 gp_Ax2d
转换构造
示例:
Handle ( Geom2d_AxisPlacement) axis = new Geom2d_AxisPlacement ( gp_Pnt2d ( 0 , 0 ) , gp_Dir2d ( 1 , 0 ) ) ;
三、常用方法解析
原点与方向操作
方法 功能 SetLocation(const gp_Pnt2d& P)
设置新原点 SetDirection(const gp_Dir2d& V)
设置新方向(自动归一化) Location()
获取当前原点 Direction()
获取当前方向
方向翻转
方法 功能 Reverse()
原地翻转方向向量 Reversed()
返回一个反向的新副本(不修改原对象)
坐标轴整体操作
方法 功能 SetAxis(const gp_Ax2d& A)
设置完整坐标轴信息 Ax2d()
返回对应的 gp_Ax2d
对象 Angle(const Handle<Geom2d_AxisPlacement>& Other)
计算两个坐标轴方向夹角(单位:弧度,范围 -π 到 π) Copy()
克隆自身副本 Transform(const gp_Trsf2d& T)
应用 2D 仿射变换(平移、旋转、镜像等)
四、代码片段示例
1. 设置位置
if ( ImGui :: SliderFloat ( "Set X" , & coord[ 0 ] , - 100.0f , 100.0f ) ) {
point2d-> SetX ( coord[ 0 ] ) ;
UpdatePoint ( context) ;
}
if ( ImGui :: SliderFloat ( "Set Y" , & coord[ 1 ] , - 100.0f , 100.0f ) ) {
point2d-> SetY ( coord[ 1 ] ) ;
UpdatePoint ( context) ;
}
2. 利用Transform旋转
void ApplyRotation ( const Handle ( AIS_InteractiveContext) & context) {
gp_Trsf2d trsf;
trsf. SetRotation ( gp_Pnt2d ( 0 , 0 ) , rotateAngle * M_PI / 180.0 ) ;
point2d-> Transform ( trsf) ;
coord[ 0 ] = static_cast < float > ( point2d-> X ( ) ) ;
coord[ 1 ] = static_cast < float > ( point2d-> Y ( ) ) ;
UpdatePoint ( context) ;
}
五、总结
特点 说明 轻量 只包含点和方向向量 操作灵活 支持方向翻转、复制、变换、方向角计算等 工程适用性强 常用于草图系统、路径定义、投影方向设定等场景
六、代码
# pragma once
# include "pch.h"
# include <Geom2d_CartesianPoint.hxx>
# include <Geom2d_Transformation.hxx>
# include <gp_Trsf2d.hxx>
# include <BRepBuilderAPI_MakeVertex.hxx>
# include <AIS_Shape.hxx>
# include "BaseScene.h"
# include "VisSceneComponents.h"
# include "TutorialWindow.h"
class Geom2d012 : public BaseScene , public VisSceneComponents , public TutorialWindow {
public :
Geom2d012 ( ) { openTutorialWindow ( ) ; }
void displayScene ( const Handle ( V3d_View) & view, const Handle ( AIS_InteractiveContext) & context) override {
if ( ! bIsSceneInit) {
sceneInit ( view, context) ;
bIsSceneInit = true ;
}
renderTutorialWindow ( context) ;
}
void customInitTutorialWindow ( const Handle ( AIS_InteractiveContext) & context) override { }
void sceneInit ( const Handle ( V3d_View) & , const Handle ( AIS_InteractiveContext) & context) override {
point2d = new Geom2d_CartesianPoint ( 0.0 , 0.0 ) ;
VisualizePoint ( context) ;
}
void renderTutorialContent ( const Handle ( AIS_InteractiveContext) & context) override {
ImGui :: Text ( "Geom2d_CartesianPoint Controls" ) ;
if ( ImGui :: SliderFloat ( "Set X" , & coord[ 0 ] , - 100.0f , 100.0f ) ) {
point2d-> SetX ( coord[ 0 ] ) ;
UpdatePoint ( context) ;
}
if ( ImGui :: SliderFloat ( "Set Y" , & coord[ 1 ] , - 100.0f , 100.0f ) ) {
point2d-> SetY ( coord[ 1 ] ) ;
UpdatePoint ( context) ;
}
ImGui :: Separator ( ) ;
ImGui :: Text ( "Transformation:" ) ;
if ( ImGui :: SliderFloat ( "Rotate Angle (deg)" , & rotateAngle, 0.0f , 360.0f ) ) { }
if ( ImGui :: Button ( "Apply Rotation" ) ) {
ApplyRotation ( context) ;
}
if ( ImGui :: Button ( "Reset Point" ) ) {
point2d-> SetCoord ( 0.0 , 0.0 ) ;
coord[ 0 ] = 0.0f ;
coord[ 1 ] = 0.0f ;
UpdatePoint ( context) ;
}
ImGui :: Separator ( ) ;
ImGui :: Text ( "Current Coordinates:" ) ;
double x = point2d-> X ( ) ;
double y = point2d-> Y ( ) ;
ImGui :: Text ( "X: %.2f, Y: %.2f" , x, y) ;
}
private :
Handle ( Geom2d_CartesianPoint) point2d;
Handle ( AIS_Shape) pointAIS;
float coord[ 2 ] = { 0.0f , 0.0f } ;
float rotateAngle = 45.0f ;
void VisualizePoint ( const Handle ( AIS_InteractiveContext) & context) {
gp_Pnt p3d ( point2d-> X ( ) , point2d-> Y ( ) , 0.0 ) ;
TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex ( p3d) ;
pointAIS = new AIS_Shape ( vertex) ;
pointAIS-> SetColor ( Quantity_NOC_RED) ;
context-> Display ( pointAIS, Standard_False) ;
}
void UpdatePoint ( const Handle ( AIS_InteractiveContext) & context) {
gp_Pnt p3d ( point2d-> X ( ) , point2d-> Y ( ) , 0.0 ) ;
TopoDS_Vertex vertex = BRepBuilderAPI_MakeVertex ( p3d) ;
pointAIS-> SetShape ( vertex) ;
context-> Redisplay ( pointAIS, Standard_False) ;
}
void ApplyRotation ( const Handle ( AIS_InteractiveContext) & context) {
gp_Trsf2d trsf;
trsf. SetRotation ( gp_Pnt2d ( 0 , 0 ) , rotateAngle * M_PI / 180.0 ) ;
point2d-> Transform ( trsf) ;
coord[ 0 ] = static_cast < float > ( point2d-> X ( ) ) ;
coord[ 1 ] = static_cast < float > ( point2d-> Y ( ) ) ;
UpdatePoint ( context) ;
}
} ;