0

0

分享

【初级教程七】倒角、建孔

25 0
5 天前 天工开发者中心| 显示全部楼层 阅读模式
本帖最后由 天工开发者团队 于 2024-10-17 16:06 编辑

一、前置性代码
         创建一个矩形

  1. // 创建一个中点为(0,0),长度为0.1,宽度为0.08的矩形
  2. void CreateRectangle(TGPart::ProfilePtr& pProfile, double cenX = 0.0
  3.             , double cenY = 0.0, double len = 0.1, double width = 0.08);
复制代码
  1. void CreateRectangle(TGPart::ProfilePtr& pProfile, double cenX, double cenY, double len, double width)
  2. {
  3.     // 添加矩形
  4.     TGFrameworkSupport::Lines2dPtr pLines2d = pProfile->GetLines2d();
  5.     std::array<TGFrameworkSupport::Line2dPtr, 4> lines;
  6.     lines[0] = pLines2d->AddBy2Points(cenX - len / 2, cenY - width / 2, cenX + len / 2, cenY - width / 2);// 底边
  7.     lines[1] = pLines2d->AddBy2Points(cenX + len / 2, cenY - width / 2, cenX + len / 2, cenY + width / 2); // 右边
  8.     lines[2] = pLines2d->AddBy2Points(cenX + len / 2, cenY + width / 2, cenX - len / 2, cenY + width / 2); // 上边
  9.     lines[3] = pLines2d->AddBy2Points(cenX - len / 2, cenY + width / 2, cenX - len / 2, cenY - width / 2); // 左边

  10.     TGFrameworkSupport::Relations2dPtr pRelations2d = pProfile->GetRelations2d();
  11.     for (int i = 0; i < 4; i++)
  12.     {
  13.         TGFrameworkSupport::Line2dPtr pLine = lines[i];
  14.         TGFrameworkSupport::Line2dPtr pNextLine = lines[(i + 1) % 4];
  15.         pRelations2d->AddKeypoint(
  16.             pLine,
  17.             (int)TGConstants::KeypointIndexConstants::igLineEnd,
  18.             pNextLine,
  19.             (int)TGConstants::KeypointIndexConstants::igLineStart,
  20.             true);
  21.     }
  22. }
复制代码
       创建一个一维数组
  1. template<typename T>
  2. _variant_t CreateVariant(T* t, VARENUM em)
  3. {
  4.     // 暂时支持数量为一的一维数组
  5.     SAFEARRAYBOUND bound = { 1, 0 };
  6.     SAFEARRAY* pSafeArry = SafeArrayCreate(em, 1, &bound);
  7.     SafeArrayPutElement(pSafeArry, &bound.lLbound, t);
  8.     VARIANT var;
  9.     VariantInit(&var);
  10.     var.vt = VT_ARRAY | em;
  11.     var.parray = pSafeArry;
  12.     return _variant_t(var);
  13. }
复制代码

二、孔
         概念:使用孔命令可以构造简单孔、螺纹孔、沉头孔、埋头孔以及锥孔。

5.1.png

         API:
  1. // API 所在数据结构 Holes
  2. HolePtr AddFinite (// 轮廓,传洞口hole2d结构
  3. struct Profile * Profile,
  4. // 孔的方向igLeft基于平面法向igRight基于平面法向反向(平面指的是参考平面)
  5. enum FeaturePropertyConstants ProfilePlaneSide,
  6. // 孔深度
  7. double FiniteDepth,
  8. // 孔数据,包括孔类型,以及沉头孔和螺纹孔的其他参数
  9. struct HoleData * Data );
复制代码

         使用示例:
  1. void AddHole()
  2. {
  3.     Application* application = TGAddinApp::GetTGApp()->GetApplication();
  4.     TGPart::PartDocumentPtr pDoc = application->GetActiveDocument();
  5.     // 新建草图
  6.     TGPart::SketchPtr pSketch = pDoc->Sketches->Add();
  7.     //参考平面
  8.     TGPart::RefPlanePtr pRefplane = pDoc->GetRefPlanes()->Item(3);
  9.     // 轮廓
  10.     TGPart::ProfilePtr pProfile = pSketch->GetProfiles()->Add(pRefplane);
  11.     // 创建一个矩形
  12.     CreateRectangle(pProfile);
  13.     // 拉伸
  14.     CComSafeArray<IDispatch*> aProfiles(1);
  15.     aProfiles[0] = pProfile;
  16.     // 基于不同的轮廓去进行拉伸
  17.     pDoc->Models->AddFiniteExtrudedProtrusion(
  18.         1,
  19.         aProfiles.GetSafeArrayPtr(),
  20.         TGPart::FeaturePropertyConstants::igRight,
  21.         0.2);
  22.     // 添加2D洞口
  23.     TGPart::ProfilePtr pHolePro = pDoc->Sketches->Add()->GetProfiles()->Add(pDoc->GetRefPlanes()->Item(2));
  24.     pHolePro->Holes2d->Add(-0.025, 0.03);
  25.     auto status = pHolePro->End(TGPart::ProfileValidationType::igProfileClosed);
  26.     // 获取孔数据并添加新孔
  27.     TGPart::HoleDataCollectionPtr pHoleCol = pDoc->GetHoleDataCollection();
  28.   //
  29.     HoleDataPtr pHoleData = pHoleCol->Add(FeaturePropertyConstants::igRegularHole, 0.006);
  30.     pDoc->Models->Item(1)->Holes->AddFinite(pHolePro, FeaturePropertyConstants::igBoth, 0.1, pHoleData);
  31. }
复制代码

         代码效果图:

5.2.png
三、槽
         概念:使用槽命令,在直接建模或顺序建模零件和钣金环境中,沿相切连续的草图方向创建槽特征。

5.3.png

         API:
  1. // API所在数据结构 Slots
  2. SlotPtr Add (
  3. struct Profile * Profile,// 槽的轮廓
  4. enum FeaturePropertyConstants SlotType,// 槽的类型
  5. enum FeaturePropertyConstants SlotEndCondition,// 槽端部条件
  6. double SlotWidth,// 槽的宽度
  7. double SlotOffsetWidth,// 槽的偏移宽度
  8. double SlotOffsetDepth,// 槽的偏移深度
  9. enum FeaturePropertyConstants ExtentType,// 第一个范围,槽的延伸方式
  10. enum FeaturePropertyConstants ExtentSide,// 第一个范围侧
  11. double FiniteDistance,// 第一个有限距离,延伸距离
  12. enum KeyPointExtentConstants KeyPointFlags,// 关键点标识类型
  13. IDispatch * KeyPointOrTangentFace,// 关键点,无则为NULL
  14. enum FeaturePropertyConstants ExtentType2,// 第二个范围,槽的延伸方式
  15. enum FeaturePropertyConstants ExtentSide2,// 第一个范围侧
  16. double FiniteDistance2,// 第二个有限距离
  17. enum KeyPointExtentConstants KeyPointFlags2,// 第二个关键点标识
  18. IDispatch * KeyPointOrTangentFace2,// 关键点
  19. IDispatch * FromFaceOrPlane,// 面或平面定义槽的起点
  20. enum OffsetSideConstants FromOffsetSide,// 来自的偏移侧
  21. double FromOffsetDistance,// 来自的偏移距离
  22. IDispatch * ToFaceOrPlane,// 面或平面
  23. enum OffsetSideConstants ToOffsetSide,// 到达的偏移侧
  24. double ToOffsetDistance );// 到达的偏移距离
复制代码

         使用示例:
  1. void AddSlot()
  2. {
  3.     Application* application = TGAddinApp::GetTGApp()->GetApplication();
  4.     TGPart::PartDocumentPtr pDoc = application->GetActiveDocument();
  5.     // 新建草图
  6.     TGPart::SketchPtr pSketch = pDoc->Sketches->Add();
  7.     //参考平面
  8.     TGPart::RefPlanePtr pRefplane = pDoc->GetRefPlanes()->Item(3);
  9.     // 轮廓
  10.     TGPart::ProfilePtr pProfile = pSketch->GetProfiles()->Add(pRefplane);
  11.     // 创建一个矩形
  12.     CreateRectangle(pProfile);
  13.     // 拉伸
  14.     CComSafeArray<IDispatch*> aProfiles(1);
  15.     aProfiles[0] = pProfile;

  16.     // 基于不同的轮廓去进行拉伸
  17.     pDoc->Models->AddFiniteExtrudedProtrusion(
  18.         1,
  19.         aProfiles.GetSafeArrayPtr(),
  20.         TGPart::FeaturePropertyConstants::igRight,
  21.         0.2);
  22.     // 新建草图
  23.     pSketch = pDoc->Sketches->Add();
  24.     pProfile = pSketch->GetProfiles()->Add(pRefplane);
  25.     TGFrameworkSupport::Lines2dPtr pLines2d = pProfile->GetLines2d();
  26.     double cenX = 0.0;   // 中心X
  27.     double cenY = 0.0;   // 中心Y
  28.     double len = 0.1;      // 长
  29.     double width = 0.08;  // 宽
  30.       // 添加特征轮廓
  31.     pLines2d->AddBy2Points(cenX - len / 2, cenY, cenX + len / 2, cenY);

  32.     TGPart::SlotsPtr pSlots = pDoc->Models->Item(1)->Slots;
  33.     pSlots->Add(
  34.         pProfile, FeaturePropertyConstants::igRegularSlot, FeaturePropertyConstants::igFormedEnd,
  35.         0.005, 0.0, 0.0,
  36.         FeaturePropertyConstants::igFinite, FeaturePropertyConstants::igRight,
  37.         0.2,
  38.         KeyPointExtentConstants::igTangentNormal, NULL,
  39.         FeaturePropertyConstants::igNone, FeaturePropertyConstants::igNone,
  40.         0.0,
  41.         KeyPointExtentConstants::igTangentNormal,
  42.         NULL, NULL,
  43.         OffsetSideConstants::seOffsetNone,
  44.         0.0, NULL,
  45.         OffsetSideConstants::seOffsetNone, 0.0
  46.     );
  47. }
复制代码

         代码效果图:
5.4.png

四、倒角

4.1 倒圆角
         概念:倒圆允许您将锐利的模型边替换为平滑的倒圆表面,以改进模型外观或功能。
5.5.png
         API:
  1. RoundPtr Add(
  2. long NumberOfEdgeSets,// 倒圆边的数量
  3. const _variant_t& EdgeSetArray,// 倒圆边的数组
  4. const _variant_t& RadiusArray,// 倒圆边的对应的半径的数组
  5. const _variant_t& RollAcrossTangentEdges = vtMissing,
  6. const _variant_t& RollOrCapAcrossSharpEdges = vtMissing,
  7. const _variant_t& RollAlongBlendEdges = vtMissing,
  8. const _variant_t& ApplyVertexBlends = vtMissing);
复制代码

         使用示例:
  1. void AddRound()
  2. {
  3.     Application* application = TGAddinApp::GetTGApp()->GetApplication();
  4.     TGPart::PartDocumentPtr pDoc = application->GetActiveDocument();
  5.     // 新建草图
  6.     TGPart::SketchPtr pSketch = pDoc->Sketches->Add();
  7.     //参考平面
  8.     TGPart::RefPlanePtr pRefplane = pDoc->GetRefPlanes()->Item(3);
  9.     // 轮廓
  10.     TGPart::ProfilePtr pProfile = pSketch->GetProfiles()->Add(pRefplane);
  11.     // 创建一个中线点为(0,0),长度为0.1,宽度为0.08的矩形
  12.     CreateRectangle(pProfile);
  13.     // 拉伸
  14.     CComSafeArray<IDispatch*> aProfiles(1);
  15.     aProfiles[0] = pProfile;
  16.     // 基于矩形进行拉伸 ,右侧,长度为0.2
  17.     pDoc->Models->AddFiniteExtrudedProtrusion(
  18.         1,
  19.         aProfiles.GetSafeArrayPtr(),
  20.         TGPart::FeaturePropertyConstants::igRight,
  21.         0.2);
  22.     // 选择可以倒圆的边
  23.     ExtrudedProtrusionPtr pEx = pDoc->Models->Item(1)->ExtrudedProtrusions->Item(1);
  24.     TGGeometry::EdgesPtr pEdges =
  25.         pEx->Edges[TGGeometry::FeatureTopologyQueryTypeConstants::igQueryRoundable];
  26.     IDispatchPtr s = pEdges->Item(1);
  27.     //倒圆半径 0.05
  28.     double radius = 0.05;
  29.     // 构造参数
  30.     _variant_t    edgeSetArray = CreateVariant<IDispatch>(s, VT_DISPATCH);
  31.     _variant_t    radiusArray = CreateVariant<double>(&radius, VT_R8);
  32.     // 添加倒圆
  33.     RoundsPtr pRounds = pDoc->Models->Item(1)->GetRounds();
  34.     RoundPtr pRound = pRounds->Add(
  35.         1, edgeSetArray, radiusArray
  36.     );
  37. }
复制代码

         代码效果图:
5.6.png
4.2 倒斜角
         概念:使用倒斜角命令(特征) 可在两个面之间 公共边上构造倒斜角。
5.7.png
         API:
  1. ChamferPtr AddEqualSetback(
  2. long NumberOfEdgeSets,//  边的数量
  3. const _variant_t& EdgeSetArray,// 边的数组 IDispatch类型
  4. double SetbackDistance);// 倒斜角距离
复制代码
         使用示例:
  1. void AddChamfer()
  2. {
  3.     Application* application = TGAddinApp::GetTGApp()->GetApplication();
  4.     TGPart::PartDocumentPtr pDoc = application->GetActiveDocument();
  5.     // 新建草图
  6.     TGPart::SketchPtr pSketch = pDoc->Sketches->Add();
  7.     //参考平面
  8.     TGPart::RefPlanePtr pRefplane = pDoc->GetRefPlanes()->Item(3);
  9.     // 轮廓
  10.     TGPart::ProfilePtr pProfile = pSketch->GetProfiles()->Add(pRefplane);
  11.     // 创建一个中线点为(0,0),长度为0.1,宽度为0.08的矩形
  12.     CreateRectangle(pProfile);
  13.     // 拉伸
  14.     CComSafeArray<IDispatch*> aProfiles(1);
  15.     aProfiles[0] = pProfile;
  16.     // 基于矩形去进行拉伸
  17.     pDoc->Models->AddFiniteExtrudedProtrusion(
  18.         1,
  19.         aProfiles.GetSafeArrayPtr(),
  20.         TGPart::FeaturePropertyConstants::igRight,
  21.         0.2);
  22.     // 选择可以倒斜角的边
  23.     ExtrudedProtrusionPtr pEx = pDoc->Models->Item(1)->ExtrudedProtrusions->Item(1);
  24.     TGGeometry::EdgesPtr pEdges =
  25.         pEx->Edges[TGGeometry::FeatureTopologyQueryTypeConstants::igQueryRoundable];
  26.     IDispatchPtr s = pEdges->Item(2);
  27.     //倒斜角距离 0.01
  28.     double distance = 0.01;
  29.     // 构造参数
  30.     _variant_t    edgeSetArray = CreateVariant<IDispatch>(s, VT_DISPATCH);
  31.     // 获取斜角集合
  32.     ChamfersPtr pChamfers = pDoc->Models->Item(1)->GetChamfers();
  33.     // AddEqualSetback 从边缘集数组创建一个或多个倒角对象。
  34.     // AddUnequalSetback 以指定角度创建一个或多个倒角对象
  35.     // AddSetbackAngle 创建一个或多个后退距离不等的倒角对象
  36.     ChamferPtr pChamfer = pChamfers->AddEqualSetback(
  37.         1,
  38.         edgeSetArray,
  39.         distance
  40.     );
  41. }
复制代码

         代码效果图:
5.8.png

评论(0)

您需要登录后才可以回复 登录

客服 意见反馈
返回顶部
快速回复 返回顶部 返回列表