AMCAX Kernel
Geometry kernel for CAD/CAE/CAM
九韶内核 1.0.0.0
载入中...
搜索中...
未找到
自底向上构建简单模型

概述

本教程提供了如何自底向上建模,将会以构建一个正方体为例进行讲解。

构建一个正方体正确的做法应该是:构建 8 个 Vertex,再使用 8 个 Vertex 构建 12 个 Edge,再用 Edge 构建 Wire 和 Face。以下是本次要构建的正方体的 Vertex、Edge、Face 编号,以方便您的理解。

注:图片中的箭头表示 edge 的方向。

构建正方体

构建 Vertex

std::vector<AMCAX::TopoShape> BuildBoxVertices()
{
// Define the 8 corner points of a box
std::vector<AMCAX::Point3> points;
points.push_back(AMCAX::Point3(0.5, -0.5, -0.5));
points.push_back(AMCAX::Point3(-0.5, -0.5, -0.5));
points.push_back(AMCAX::Point3(-0.5, 0.5, -0.5));
points.push_back(AMCAX::Point3(0.5, 0.5, -0.5));
points.push_back(AMCAX::Point3(0.5, -0.5, 0.5));
points.push_back(AMCAX::Point3(-0.5, -0.5, 0.5));
points.push_back(AMCAX::Point3(-0.5, 0.5, 0.5));
points.push_back(AMCAX::Point3(0.5, 0.5, 0.5));
std::vector<AMCAX::TopoShape> vertices;
for (const AMCAX::Point3& p : points)
{
vertices.push_back(v);
}
return vertices;
}
static constexpr double Confusion() noexcept
获取混淆容差
定义 Precision.hpp:122
构造 B-Rep 结构的工具类
定义 TopoBuilder.hpp:37
AMCAX_API void MakeVertex(TopoVertex &v) const
构造空顶点
顶点类
定义 TopoVertex.hpp:12
PointT< double, 3 > Point3
三维点
定义 PointT.hpp:459

构建 Edge

std::vector<AMCAX::TopoShape> BuildBoxEdges(const std::vector<AMCAX::TopoShape>& vertices)
{
// Vertex index pairs defining all box edges
std::vector<std::pair<int, int>> edgeVerticesID = { {0,1},{1,2},{2,3},{3,0},{4,5},{5,6},{6,7},{7,4},{0,4},{1,5},{2,6},{3,7} };
std::vector<AMCAX::TopoShape> edges;
for (const auto& [beginVID, endVID] : edgeVerticesID)
{
// Get oriented vertices (forward/reversed)
AMCAX::TopoVertex v1 = AMCAX::TopoCast::Vertex(vertices[beginVID].Oriented(AMCAX::OrientationType::Forward));
AMCAX::TopoVertex v2 = AMCAX::TopoCast::Vertex(vertices[endVID].Oriented(AMCAX::OrientationType::Reversed));
// Create 3D curve
AMCAX::Direction3 dir(p2.Coord() - p1.Coord());
std::shared_ptr<AMCAX::Geom3Curve> gline = std::make_shared<AMCAX::Geom3Line>(p1, dir);
// Build edge
builder.MakeEdge(edge, gline, AMCAX::Precision::Confusion());
builder.Add(edge, v1);
builder.UpdateVertex(v1, 0.0, edge, AMCAX::Precision::Confusion());
builder.Add(edge, v2);
builder.UpdateVertex(v2, p1.Distance(p2), edge, AMCAX::Precision::Confusion());
edges.push_back(edge);
}
return edges;
}
auto Distance(const PointT< OtherScalar, DIM > &other) const noexcept
计算该点与其他点的欧几里得距离
定义 PointT.hpp:180
constexpr const CoordType & Coord() const noexcept
获取点的坐标
定义 PointT.hpp:151
AMCAX_API void MakeEdge(TopoEdge &e) const
构造空边
AMCAX_API void Add(TopoShape &s, const TopoShape &c) const
向形状中添加子形状
AMCAX_API void UpdateVertex(const TopoVertex &v, const Point3 &p, double tol) const
更新顶点的点
static AMCAX_API const TopoVertex & Vertex(const TopoShape &s)
将形状转换为顶点
边的类
定义 TopoEdge.hpp:12
static AMCAX_API Point3 Point(const TopoVertex &v)
获取顶点的几何点
DirectionT< double, 3 > Direction3
三维方向
定义 DirectionT.hpp:587

在构建 edge 时需要注意:在给 edge 添加 vertex 时,需要提前设置 vertex 的 Orientation,因为 Edge 的起始 Vertex 是 Forward,终止 Vertex 是 Reversed。

构建 Wire

std::vector<AMCAX::TopoShape> BuildBoxWires(const std::vector<AMCAX::TopoShape>& edges)
{
// Edge connectivity for each face wire: {edge index, orientation} pairs (true=forward, false=reversed)
std::vector<std::vector<std::pair<int, bool>>> wireEdgesVec =
{
{ {0,true},{1,true},{2,true},{3,true} },
{ {4,false},{7,false},{6,false},{5,false} },
{ {4,true},{9,false},{0,false},{8,true} },
{ {6,true},{11,false},{2,false},{10,true} },
{ {3,false},{11,true},{7,true},{8,false} },
{ {5,true},{10,false},{1,false},{9,true} }
};
std::vector<AMCAX::TopoShape> wires;
for (const auto& wireEdges : wireEdgesVec)
{
builder.MakeWire(wire);
for (const auto& [edgeID, isForward] : wireEdges)
{
// Add edge with proper orientation
AMCAX::TopoShape crshape = edges[edgeID];
if (isForward)
{
edge = AMCAX::TopoCast::Edge(crshape.Oriented(AMCAX::OrientationType::Forward));
}
else
{
edge = AMCAX::TopoCast::Edge(crshape.Oriented(AMCAX::OrientationType::Reversed));
}
builder.Add(wire, edge);
}
// Ensure wire closure
bool isClosedWire = AMCAX::TopoTool::IsClosed(wire);
wire.Closed(isClosedWire);
wires.push_back(wire);
}
return wires;
}
AMCAX_API void MakeWire(TopoWire &w) const
构造一个空环
static AMCAX_API const TopoEdge & Edge(const TopoShape &s)
将形状转换为边
形状的基类,包含具有位置和方向信息的基础形状
定义 TopoShape.hpp:15
AMCAX_API TopoShape Oriented(OrientationType orient) const
获取指定方向后的形状
AMCAX_API bool Closed() const
判断形状是否封闭
static AMCAX_API bool IsClosed(const TopoShape &s)
判断形状是否封闭
环类
定义 TopoWire.hpp:12

在构建 wire 时需要注意:所有 wire 必须遵循逆时针方向​(CCW),观察视角应为面法向的反向​(即从实体外部看向内部)。

构建 Face

std::vector<AMCAX::TopoShape> BuildBoxFaces(const std::vector<AMCAX::TopoShape>& wires)
{
// Create reference frames for each face plane
AMCAX::Frame3 frameBottom(AMCAX::Point3(0.0, 0.0, -0.5), -z, -x);
AMCAX::Frame3 frameTop(AMCAX::Point3(0.0, 0.0, 0.5), z, x);
AMCAX::Frame3 frameLeft(AMCAX::Point3(0.0, -0.5, 0.0), -y, -x);
AMCAX::Frame3 frameRight(AMCAX::Point3(0.0, 0.5, 0.0), y, x);
AMCAX::Frame3 frameFront(AMCAX::Point3(0.5, 0.0, 0.0), x, -z);
AMCAX::Frame3 frameBack(AMCAX::Point3(-0.5, 0.0, 0.0), -x, z);
std::vector<AMCAX::Frame3> planeFrames = { frameBottom,frameTop,frameLeft,frameRight,frameFront,frameBack };
// Create 3D plane
std::vector<std::shared_ptr<AMCAX::Geom3Plane>> planes;
for (const AMCAX::Frame3& frame : planeFrames)
{
planes.push_back(std::make_shared<AMCAX::Geom3Plane>(frame));
}
std::vector<AMCAX::TopoShape> faces;
for (int i = 0; i < static_cast<int>(wires.size()); i++)
{
//Make a face with a surface
builder.MakeFace(face, planes[i], AMCAX::Precision::Confusion());
builder.Add(face, wires[i]);
faces.push_back(face);
}
return faces;
}
static AMCAX_API const Direction3 & DY() noexcept
获取三维中的 y 方向
static AMCAX_API const Direction3 & DZ() noexcept
获取三维中的 z 方向
static AMCAX_API const Direction3 & DX() noexcept
获取三维中的 x 方向
AMCAX_API void MakeFace(TopoFace &f) const
构造一个空面
面类
定义 TopoFace.hpp:12
FrameT< double, 3 > Frame3
三维标架
定义 FrameT.hpp:885

构建 Shell

std::vector<AMCAX::TopoShape> vertices = BuildBoxVertices();
std::vector<AMCAX::TopoShape> edges = BuildBoxEdges(vertices);
std::vector<AMCAX::TopoShape> wires = BuildBoxWires(edges);
std::vector<AMCAX::TopoShape> faces = BuildBoxFaces(wires);
//Make a shell and add all faces in the shell
builder.MakeShell(shell);
for (const auto& face : faces)
{
builder.Add(shell, face);
}
AMCAX_API void MakeShell(TopoShell &s) const
构造一个空壳
壳体类
定义 TopoShell.hpp:12