【初级教程四】常用三维图形创建
本帖最后由 天工开发者团队 于 2024-10-23 16:51 编辑一、点
通过x y z坐标可创建点。
API:
Point3DPtr Add(long NumberOfValues, SAFEARRAY** Array); 参数:
NumberOfValues:Array 的个数
Array: 点集,按顺序每三个元素形成一个点,分别是x、y、z
示例:
ATL::CComSafeArray<T> ToComSafeArray(std::initializer_list<T> const& initial_list)
{
ATL::CComSafeArray<T> comArray((ULONG)initial_list.size());
long i = 0;
for (auto it = initial_list.begin(); it != initial_list.end(); ++it)
{
comArray.SetAt(i, *it);
i++;
}
return comArray;
}
bool CreatePoint()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建三维草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
//创建点
TGPart::Points3DPtr points = sketch->GetPoints3D();
ATL::CComSafeArray<double> point3DArray = ToComSafeArray({ 0.0,0.0,0.0,0.03,0.04,0.05,0.02,0.03,0.04 });
TGPart::Point3DPtr point = points->Add(9, point3DArray.GetSafeArrayPtr());
return true;
} 上述代码创建了三个点,分别是(0,0,0),(0.03,0.04,0.05),(0.02,0.03,0.04),效果如下:
二、线段
通过两个点可以确定一个直线。
API:
Line3DPtr Add(double Startx, double Starty, double Startz, double Endx, double Endy, double Endz); 参数:
Startx、Starty、Startz: 起点坐标
Endx、Endy、Endz: 终点坐标
通过两个点画一个线段,示例如下:
bool CreateLine()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
//创建起点为(-0.01, 0.03, 0.15)终点为(0.05, 0.06, 0.07)的线
TGPart::Lines3DPtr lines = sketch->GetLines3D();
TGPart::Line3DPtr line = lines->Add(-0.01, 0.03, 0.15, 0.05, 0.06, 0.07);
return true;
} 效果如下:
API:
HRESULT AddEx(SAFEARRAY** arrPoints, SAFEARRAY** arrLines); 参数:
arrPoints:点集,为3的倍数,按顺序三个元素分别为一个点的x、y、z坐标
arrLines: 线集(出参)
把所有点按顺序连接成线段,示例如下:
bool CreateLine()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
//添加连接点集(0.5,0.1,0.2),(0.8,0.6,0.5),(0.6,0.7,0.8),(0.09,0.11,0.17)的连段
ATL::CComSafeArray<double> points = ToComSafeArray({ 0.5,0.1,0.2,0.8,0.6,0.5,0.6,0.7,0.8,0.09,0.11,0.17 });
ATL::CComSafeArray<IDispatch*> lines(3);
sketch->AddEx(points.GetSafeArrayPtr(), lines.GetSafeArrayPtr());
return true;
} 效果如下:
三、矩形
通过长宽和中心点、法向、平面U方向可以确定一个矩形
API:
TGPart::Lines3D::CreateRectangle
(double dWidth, double dHeight,
double dCentrexpos, double dCentreypos, double dCentrezpos,
double dPlaneNormalxpos, double dPlaneNormalypos, double dPlaneNormalzpos,
double dPlaneUDirxpos, double dPlaneUDirypos, double dPlaneUDirzpos,
SAFEARRAY** Line3D); 参数:
dWidth: 宽
dHeight: 高
dCentrexpos、dCentreypos、dCentrezpos: 中心点
dPlaneNormalxpos、dPlaneNormalypos、dPlaneNormalzpos: 法向
Line3D: 矩形的四个线段(输出参数)
代码示例:
bool CreateRect(TGPart::Sketch3DPtr const& sketch)
{
TGPart::Lines3DPtr lines = sketch->GetLines3D();
ATL::CComSafeArray<IDispatch*> toPointObjs(4);
LPSAFEARRAY pSafeArray = toPointObjs.Detach();
//长为0.03,宽为0.04,中心点为(0.01,0.05,0.08) 法向为(1,1,1) 方向为(1,0,1)
lines->CreateRectangle(0.03, 0.04, 0.01, 0.05, 0.08, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, &pSafeArray);
toPointObjs.Attach(pSafeArray);
return true;
} 运行效果:
四、圆
通过圆心和半径、法向可以确定一个圆。
API:
TGPart::Ellipse3DPtr TGPart::Ellipses3D::AddByCenterRadiusNormal(double CenterX, double CenterY, double CenterZ, double DirNormalX, double DirNormalY, double DirNormalZ, double Radius); 参数:
CenterX、CenterY、CenterZ: 定义圆心
DirNormalX、DirNormalY、DirNormalZ:定义法向
Radius: 定义半径
代码示例:bool CreateCircle()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
//创建圆:圆心(0.01,0.02,0.05),法向(1,1,1),半径0.04
TGPart::Ellipses3DPtr ellipses = sketch->GetEllipses3D();
TGPart::Ellipse3DPtr circle = ellipses->AddByCenterRadiusNormal(0.01, 0.02, 0.05, 1, 1, 1, 0.04);
return true;
} 运行效果:
五、圆弧
API:TGPart::Arc3DPtr TGPart::Arcs3D::AddByStartEndCenterNormal
(double Startx, double Starty, double Startz,
double Endx, double Endy, double Endz,
double CenterX, double CenterY, double CenterZ,
double NormalX, double NormalY, double NormalZ,
double dRadius); 参数:
Startx、Starty、Startz: 起点坐标
Endx、Endy、Endz: 终点坐标
CenterX、CenterY、CenterZ: 圆心
NormalX、NormalY、NormalZ:法向
dRadius: 半径
代码示例:bool CreateArc()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
//创建弧起点为(0.04254, 0.0013, 0.03616),终点为(0.00312,-0.00421, 0.08109),圆心为(0.01, 0.02, 0.05),法向(1, 1, 1),半径0.04
TGPart::Arcs3DPtr arcs = sketch->GetArcs3D();
TGPart::Arc3DPtr arc = arcs->AddByStartEndCenterNormal(0.04254, 0.0013, 0.03616, 0.00312, -0.00421, 0.08109, 0.01, 0.02, 0.05, 1, 1, 1, 0.04);
return true;
} 运行效果:
六、样条曲线
所谓样条曲线(Spline Curves)是指给定一组控制点而得到一条曲线,曲线的大致形状由这些点予以控制,一般可分为插值样条和逼近样条两种,插值样条通常用于数字化绘图或动画的设计,逼近样条一般用来构造物体的表面。
API:
TGPart::BSplineCurve3DPtr TGPart::BSplineCurves3D::AddByPoints(long ArraySize, SAFEARRAY** Array, VARIANT_BOOL bCreateClosedCurve); 参数:
ArraySize: 点个数
Array: 点坐标集
bCreateClosedCurve: 是否闭合,true闭合,false不闭合
代码示例:bool CreateSplineCurves()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
ATL::CComSafeArray<double> points = ToComSafeArray({ -0.15411270107593356, -0.068218488561353263, -0.026578103946878956, -0.10365533369715851,
-0.015327208266761847, 0.063058046076703067, -0.018732188479349772, -0.013590396773229624, -0.017264793939970029, -0.1114395643167483, -0.06201958271901041,
-0.052832153874633456, -0.13789130547567818, -0.0678929518988939, -0.041937248227906225, -0.10411270107593357, -0.068218488561353263, -0.026578103946878956 });
//创建点--用于比较样点是否在曲线上
TGPart::Points3DPtr pPoints = sketch->GetPoints3D();
TGPart::Point3DPtr point = pPoints->Add(18, points.GetSafeArrayPtr());
//创建样条曲线
TGPart::BSplineCurves3DPtrsplines = sketch->GetBSplineCurves3D();
TGPart::BSplineCurve3DPtr spline = splines->AddByPoints(6, points.GetSafeArrayPtr(), true);
return true;
} 运行效果:
由效果图可知,点都在样条曲线上,AddByPoints方法用于插值样条曲线。
七、圆角
API:TGPart::Arc3DPtr TGPart::Arcs3D::AddAsFillet(IDispatch* Obj1, IDispatch* Obj2, double dRadius, VARIANT_BOOL bNoTrim); 参数:
Obj1、Obj2 : 线段
dRadius : 半径
bNoTrim: 是否修剪 true(不修剪)、false(修剪)
代码示例:
bool CreateFillet()
{
// 获取Application
Application* application = TGAddinApp::GetTGApp()->GetTGAddin()->GetApplication();
// 获取PartDocumentPtr
TGPart::PartDocumentPtr partDoc = application->Documents->Add("SolidEdge.PartDocument");
assert(partDoc);
//创建草图
Sketches3DPtr sketches = partDoc->GetSketches3D();
Sketch3DPtr sketch = sketches->Add();
TGPart::Arcs3DPtr arcs = sketch->GetArcs3D();
TGPart::Lines3DPtr lines = sketch->GetLines3D();
//创建圆角
TGPart::Line3DPtr line1 = lines->Add(-0.02266, 0.00805, 0.09461, -0.02266, 0.0673, 0.03536);
TGPart::Line3DPtr line2 = lines->Add(-0.02266, 0.00805, 0.09461, 0.03595, -0.02126, 0.0653);
TGPart::Arc3DPtr arc = arcs->AddAsFillet(line1, line2, 0.04, VARIANT_TRUE);
return true;
} 运行效果:
页:
[1]