0

0

分享

【初级教程十】除料

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

一、扫描除料
         概念:使用“扫描除料”命令,可以沿着您定义的路径 (B) 延伸横截面 (A),以此构造除料。
1.png
         API:
  1. SweptCutoutPtr Add(
  2.     long NumCurves,// 指定在构建扫描凸出特征时使用的路径曲线对象的数量
  3.     const _variant_t& TraceCurves,// 包含轮廓和/或边缘对象。数组的大小等于NumCurves的值
  4.     const _variant_t& TraceCurveTypes,// 数组的大小等于NumCurves的值。常用(igProfileBasedCrossSection, igEdgeBasedCrossSection)的成员
  5.     long NumSections,// 指定在构造扫描凸出特征时使用的截面对象的数量
  6.     const _variant_t& CrossSections,// 包含截面轮廓和/或边缘对象。数组的大小等于NumSections的值
  7.     const _variant_t& CrossSectionTypes,// 数组的大小等于NumSections的值
  8.     const _variant_t& Origins,// 截面原点,若截面为圆,此处为零
  9.     const _variant_t& SegmentMaps,
  10.     enum FeaturePropertyConstants MaterialSide,
  11.     enum FeaturePropertyConstants StartExtentType,
  12.     double StartExtentDistance,
  13.     IDispatch* StartSurfaceOrRefPlane,
  14.     enum FeaturePropertyConstants EndExtentType,
  15.     double EndExtentDistance,
  16.     IDispatch* EndSurfaceOrRefPlane);
复制代码
         示例代码:
  1. void AddSweptCutout()
  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.     TGFrameworkSupport::Lines2dPtr pLines2d = pProfile->GetLines2d();
  12.     // 创建一个中线点为(0.05, 0.05), 长度为0.1,宽度为0.08的矩形
  13.     double cenX = 0.0, cenY = 0.0, width = 0.08, len = 0.1;
  14.     std::array<TGFrameworkSupport::Line2dPtr, 4> lines;
  15.     lines[0] = pLines2d->AddBy2Points(cenX - len / 2, cenY - width / 2, cenX + len / 2, cenY - width / 2);// 底边
  16.     lines[1] = pLines2d->AddBy2Points(cenX + len / 2, cenY - width / 2, cenX + len / 2, cenY + width / 2); // 右边
  17.     lines[2] = pLines2d->AddBy2Points(cenX + len / 2, cenY + width / 2, cenX - len / 2, cenY + width / 2); // 上边
  18.     lines[3] = pLines2d->AddBy2Points(cenX - len / 2, cenY + width / 2, cenX - len / 2, cenY - width / 2); // 左边

  19.     TGFrameworkSupport::Relations2dPtr pRelations2d = pProfile->GetRelations2d();
  20.     for (int i = 0; i < 4; i++)
  21.     {
  22.         TGFrameworkSupport::Line2dPtr pLine = lines[i];
  23.         TGFrameworkSupport::Line2dPtr pNextLine = lines[(i + 1) % 4];
  24.         pRelations2d->AddKeypoint(
  25.             pLine,
  26.             (int)TGConstants::KeypointIndexConstants::igLineEnd,
  27.             pNextLine,
  28.             (int)TGConstants::KeypointIndexConstants::igLineStart,
  29.             true);
  30.     }
  31.     CComSafeArray<IDispatch*> aProfiles(1);
  32.     aProfiles[0] = pProfile;
  33.     // 基于矩形去进行拉伸,右侧拉伸、长度为0.05
  34.     TGPart::ModelPtr model = pDoc->Models->AddFiniteExtrudedProtrusion(
  35.         1,
  36.         aProfiles.GetSafeArrayPtr(),
  37.         TGPart::FeaturePropertyConstants::igRight,
  38.         0.05);

  39.     // 将矩形的草图作为路径曲线
  40.     IDispatchPtr pTempTrace = static_cast<IDispatch*>(pProfile);
  41.     _variant_t TraceCurves = CreateVariant<IDispatch>(pTempTrace, VT_DISPATCH);
  42.     int TraceCurveType
  43.         = TGConstants::FeaturePropertyConstants::igProfileBasedCrossSection;
  44.     _variant_t TraceCurveTypes = CreateVariant<int>(&TraceCurveType, VT_INT);

  45.     // 绘制截面轮廓
  46.     pSketch = pDoc->Sketches->Add();
  47.     ProfilePtr pProfileCross = pSketch->GetProfiles()->Add(pDoc->GetRefPlanes()->Item(2));
  48.     pProfileCross->GetCircles2d()->AddByCenterRadius(0, 0.04, 0.01);
  49.     pProfileCross->End(ProfileValidationType::igProfileClosed);

  50.     // 截面
  51.     IDispatchPtr pTempCross = static_cast<IDispatch*>(pProfileCross);
  52.     _variant_t CrossSections = CreateVariant<IDispatch>(pTempCross, VT_DISPATCH);
  53.     int CrossSectionType
  54.         = TGConstants::FeaturePropertyConstants::igProfileBasedCrossSection;
  55.     _variant_t CrossSectionTypes = CreateVariant<int>(&CrossSectionType, VT_INT);
  56.     // 截面原点,若截面为圆,此处为零
  57.     int t = 0;
  58.     _variant_t Origins = CreateVariant<int>(&t, VT_INT);
  59.     // 以圆为轮廓,矩形的草图为路径曲线进行扫掠去料
  60.     pDoc->Models->Item(1)->GetSweptCutouts()->Add(
  61.         1,
  62.         TraceCurves,
  63.         TraceCurveTypes,
  64.         1,
  65.         CrossSections,
  66.         CrossSectionTypes,
  67.         Origins,
  68.         0,
  69.         FeaturePropertyConstants::igLeft,
  70.         FeaturePropertyConstants::igNone,
  71.         0.0,
  72.         NULL,
  73.         FeaturePropertyConstants::igNone,
  74.         0.0,
  75.         NULL
  76.     );
  77. }
复制代码
         示例效果:
2.png

二、螺旋除料  
         使用螺旋除料可以沿着螺旋路径扫掠横截面,以此创建除料,主要用到的方法为HelixCutouts下的一组Add方法,下面以添加有限范围除料即AddFinite为例进行演示。其原型为
  1. HelixCutoutPtr AddFinite(struct RefAxis* HelixAxis, enum FeaturePropertyConstants AxisStart,
  2.     long NumCrossSections, SAFEARRAY** CrossSectionArray, enum FeaturePropertyConstants ProfileSide,
  3.     double Height, double Pitch, double NumberOfTurns, enum FeaturePropertyConstants HelixDir,
  4.     const _variant_t& TaperAngle = vtMissing, const _variant_t& TaperSide = vtMissing,
  5.     const _variant_t& EndPitch = vtMissing, const _variant_t& PitchRatio = vtMissing,
  6.     const _variant_t& TaperStartRadius = vtMissing, const _variant_t& TaperEndRadius = vtMissing);
复制代码
  1. void DemoAddHelixCutouts()
  2. {
  3.     namespace pt = TGPart;
  4.     namespace gm = TGGeometry;
  5.     namespace as = TGAssembly;
  6.     namespace fwp = TGFrameworkSupport;
  7.     // 建一个圆柱体用于演示除料
  8.     pt::PartDocumentPtr pPartDoc = SEAPP->GetActiveDocument();
  9.     assert(pPartDoc);
  10.     pt::ProfilePtr pProfile = pPartDoc->GetSketches()->AddByPlane(pPartDoc->GetRefPlanes()->Item(1l))->GetProfile();
  11.     pProfile->GetCircles2d()->AddByCenterRadius(0, 0, 0.05);  // 中心位于原点,半径0.5
  12.     ATL::CComSafeArray<IDispatch*> aProfiles(1);
  13.     aProfiles.SetAt(0, pProfile);
  14.     pt::ModelPtr pModel = pPartDoc->GetModels()->AddFiniteExtrudedProtrusion(aProfiles.GetCount(), aProfiles.GetSafeArrayPtr(), pt::igRight, 0.3);

  15.     // XOZ平面构造螺旋轴和截面
  16.     pt::ProfilePtr pHelixProfile = pPartDoc->GetSketches()->AddByPlane(pPartDoc->GetRefPlanes()->Item(3l))->GetProfile();
  17.     fwp::Circle2dPtr pCir2d = pHelixProfile->GetCircles2d()->AddByCenterRadius(0.05, 0, 0.01);
  18.     fwp::Line2dPtr pSeg = pHelixProfile->GetLines2d()->AddBy2Points(0, 0, 0, 0.3);
  19.     pt::RefAxisPtr pRotateAxis = pHelixProfile->SetAxisOfRevolution(pSeg);

  20.     // 进行螺旋除料
  21.     ATL::CComSafeArray<IDispatch*> crossSectionArray(1);
  22.     crossSectionArray.SetAt(0, pHelixProfile);
  23.     pModel->GetHelixCutouts()->AddFinite(pRotateAxis,  // 螺旋轴
  24.         pt::igStart,  // pt::igStart表示从轴定义的线段起点向终点向螺旋,pt::igEnd表示从轴定义的线段终点向起点螺旋
  25.         1, crossSectionArray.GetSafeArrayPtr(), // 传入横截面
  26.         pt::igRight,   // 为未来开放路径预留,暂时无用,pt::igRight和pt::igLeft为有效值
  27.         0.3,  // 螺高,暂时无用
  28.         0.1, // 螺距
  29.         3, // 螺数
  30.         pt::igLeft  // pt::igRight右旋,pt::igLeft左旋   
  31.     );
  32. }
复制代码
         上述代码演示了这个方法使用的一般过程,运行效果如下。
3.png
         可以看到一般情况下,我们只需传入旋转轴、螺旋截面、旋转方向、螺距、螺高、圈数等参数就可以创建一个螺旋除料特征,如果想使螺距、螺径变化,则还可以输入更多的参数。

三、法向除料  

         法向除料与法向拉伸类似,是通过拉伸位于零件面上的封闭曲线构造垂直于该面的除料。使用文本轮廓或其他草图元素在非平整面上构造特征时,此命令非常有用。创建一个法向除料特征,可使用NormalToFaceCutouts::Add方法,除返回值外,此方向原型与法向拉伸NormalToFaceProtrusions::Add一致。
  1. NormalToFaceCutoutPtr Add(long NumberOfInputCurves, // 闭合轮廓的拓扑边集个数
  2.     const _variant_t& InputCurvesSetArray, //闭合轮廓的拓扑边集
  3.     enum FeaturePropertyConstants LongestCurveSide, // 拉伸方向(在除料时 pt::igLeft pt::igRight未发现区别)
  4.     double offsetDistance, // 除料厚度
  5.     enum FeaturePropertyConstants FaceContainment // pt::igFacesTouchingCurvesOnly 仅修改与曲线接触的面
  6.     // pt::igAllFaces 修改曲线图内的所有面
  7. )
复制代码
  1. void DemoAddNormalCutouts()
  2. {
  3.     namespace pt = TGPart;
  4.     namespace gm = TGGeometry;
  5.     namespace as = TGAssembly;
  6.     namespace fwp = TGFrameworkSupport;
  7.     pt::PartDocumentPtr pPartDoc = SEAPP->GetActiveDocument();
  8.     assert(pPartDoc);

  9.     // 首先创建一个圆柱
  10.     pt::RefPlanePtr pYOZPlane = pPartDoc->GetRefPlanes()->Item(2l);
  11.     pt::ProfilePtr pProfile = pPartDoc->GetSketches()->AddByPlane(pYOZPlane)->GetProfile();
  12.     pProfile->GetCircles2d()->AddByCenterRadius(0.0, 0.0, 0.2);
  13.     ATL::CComSafeArray<IDispatch*> aProfiles(1);
  14.     aProfiles.SetAt(0, pProfile);
  15.     pt::ModelPtr pCylinderModel = pPartDoc->GetModels()->AddFiniteExtrudedProtrusion(1, aProfiles.GetSafeArrayPtr(),
  16.         pt::igSymmetric, 0.6);

  17.     // 在一个新的草图上创建一个圆
  18.     pt::RefPlanePtr pXOYPlane = pPartDoc->GetRefPlanes()->Item(1l);
  19.     pt::SketchPtr pSketch = pPartDoc->GetSketches()->AddByPlane(pXOYPlane);
  20.     pt::ProfilePtr pProfileScd = pSketch->GetProfile();
  21.     pProfileScd->GetCircles2d()->AddByCenterRadius(0.0, 0.0, 0.05);

  22.     // 投影圆所在草图到圆柱面上
  23.     gm::BodyPtr pBody = pCylinderModel->GetBody();
  24.     gm::FacesPtr pFaces = pBody->GetFaces(gm::igQueryCylinder);
  25.     assert(pFaces->GetCount() > 0);
  26.     gm::FacePtr pFace = pFaces->Item(1l);
  27.     long faceCnt = pFaces->GetCount();
  28.     pt::ProjectCurvesPtr pProjCvs = pPartDoc->GetConstructions()->GetProjectCurves();
  29.     pt::RefPlanePtr pDestPlane = pPartDoc->GetRefPlanes()->AddParallelByDistance(pXOYPlane, 0.2, pt::igNormalSide);
  30.     pt::ProjectCurvePtr pProjCv = pProjCvs->Add(pSketch, pFace, pDestPlane, pt::igRight, pt::igProjectOptionProject);

  31.     // 法向拉伸
  32.     gm::EdgesPtr pProjEdges = pProjCv->GetEdges(gm::igQueryAll);
  33.     long projEdgeCnt = pProjEdges->GetCount();
  34.     ATL::CComSafeArray<IDispatch*> aEdges(projEdgeCnt);
  35.     for (long i = 0; i < projEdgeCnt; i++)
  36.     {
  37.         aEdges.SetAt(i, pProjEdges->Item(i + 1));
  38.     }
  39.     _variant_t varEdges;
  40.     varEdges.parray = aEdges.Detach();
  41.     varEdges.vt = VT_ARRAY | VT_DISPATCH;
  42.     pCylinderModel->GetNormalToFaceCutouts()->Add(projEdgeCnt,  // 传入的eges个数
  43.         varEdges, // edges
  44.         pt::igLeft, // pt::igLeft 向外拉伸(加料) pt::igRight 向内拉伸(除料)
  45.         0.01,  // 除料厚度
  46.         pt::igFacesTouchingCurvesOnly // pt::igFacesTouchingCurvesOnly 仅修改与曲线接触的面
  47.         // pt::igAllFaces 修改曲线图内的所有面
  48.     );
  49. }
复制代码
         上述代码演示了使用此方向创建一个法向除料的一般过程,运行效果如下。
4.png

评论(0)

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

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