【初级教程十】除料
本帖最后由 天工开发者团队 于 2024-10-18 10:49 编辑一、扫描除料
概念:使用“扫描除料”命令,可以沿着您定义的路径 (B) 延伸横截面 (A),以此构造除料。
API:
SweptCutoutPtr Add(
long NumCurves,// 指定在构建扫描凸出特征时使用的路径曲线对象的数量
const _variant_t& TraceCurves,// 包含轮廓和/或边缘对象。数组的大小等于NumCurves的值
const _variant_t& TraceCurveTypes,// 数组的大小等于NumCurves的值。常用(igProfileBasedCrossSection, igEdgeBasedCrossSection)的成员
long NumSections,// 指定在构造扫描凸出特征时使用的截面对象的数量
const _variant_t& CrossSections,// 包含截面轮廓和/或边缘对象。数组的大小等于NumSections的值
const _variant_t& CrossSectionTypes,// 数组的大小等于NumSections的值
const _variant_t& Origins,// 截面原点,若截面为圆,此处为零
const _variant_t& SegmentMaps,
enum FeaturePropertyConstants MaterialSide,
enum FeaturePropertyConstants StartExtentType,
double StartExtentDistance,
IDispatch* StartSurfaceOrRefPlane,
enum FeaturePropertyConstants EndExtentType,
double EndExtentDistance,
IDispatch* EndSurfaceOrRefPlane); 示例代码:
void AddSweptCutout()
{
Application* application = TGAddinApp::GetTGApp()->GetApplication();
TGPart::PartDocumentPtr pDoc = application->GetActiveDocument();
// 新建草图
TGPart::SketchPtr pSketch = pDoc->Sketches->Add();
//参考平面
TGPart::RefPlanePtr pRefplane = pDoc->GetRefPlanes()->Item(3);
// 轮廓
TGPart::ProfilePtr pProfile = pSketch->GetProfiles()->Add(pRefplane);
TGFrameworkSupport::Lines2dPtr pLines2d = pProfile->GetLines2d();
// 创建一个中线点为(0.05, 0.05), 长度为0.1,宽度为0.08的矩形
double cenX = 0.0, cenY = 0.0, width = 0.08, len = 0.1;
std::array<TGFrameworkSupport::Line2dPtr, 4> lines;
lines = pLines2d->AddBy2Points(cenX - len / 2, cenY - width / 2, cenX + len / 2, cenY - width / 2);// 底边
lines = pLines2d->AddBy2Points(cenX + len / 2, cenY - width / 2, cenX + len / 2, cenY + width / 2); // 右边
lines = pLines2d->AddBy2Points(cenX + len / 2, cenY + width / 2, cenX - len / 2, cenY + width / 2); // 上边
lines = pLines2d->AddBy2Points(cenX - len / 2, cenY + width / 2, cenX - len / 2, cenY - width / 2); // 左边
TGFrameworkSupport::Relations2dPtr pRelations2d = pProfile->GetRelations2d();
for (int i = 0; i < 4; i++)
{
TGFrameworkSupport::Line2dPtr pLine = lines;
TGFrameworkSupport::Line2dPtr pNextLine = lines[(i + 1) % 4];
pRelations2d->AddKeypoint(
pLine,
(int)TGConstants::KeypointIndexConstants::igLineEnd,
pNextLine,
(int)TGConstants::KeypointIndexConstants::igLineStart,
true);
}
CComSafeArray<IDispatch*> aProfiles(1);
aProfiles = pProfile;
// 基于矩形去进行拉伸,右侧拉伸、长度为0.05
TGPart::ModelPtr model = pDoc->Models->AddFiniteExtrudedProtrusion(
1,
aProfiles.GetSafeArrayPtr(),
TGPart::FeaturePropertyConstants::igRight,
0.05);
// 将矩形的草图作为路径曲线
IDispatchPtr pTempTrace = static_cast<IDispatch*>(pProfile);
_variant_t TraceCurves = CreateVariant<IDispatch>(pTempTrace, VT_DISPATCH);
int TraceCurveType
= TGConstants::FeaturePropertyConstants::igProfileBasedCrossSection;
_variant_t TraceCurveTypes = CreateVariant<int>(&TraceCurveType, VT_INT);
// 绘制截面轮廓
pSketch = pDoc->Sketches->Add();
ProfilePtr pProfileCross = pSketch->GetProfiles()->Add(pDoc->GetRefPlanes()->Item(2));
pProfileCross->GetCircles2d()->AddByCenterRadius(0, 0.04, 0.01);
pProfileCross->End(ProfileValidationType::igProfileClosed);
// 截面
IDispatchPtr pTempCross = static_cast<IDispatch*>(pProfileCross);
_variant_t CrossSections = CreateVariant<IDispatch>(pTempCross, VT_DISPATCH);
int CrossSectionType
= TGConstants::FeaturePropertyConstants::igProfileBasedCrossSection;
_variant_t CrossSectionTypes = CreateVariant<int>(&CrossSectionType, VT_INT);
// 截面原点,若截面为圆,此处为零
int t = 0;
_variant_t Origins = CreateVariant<int>(&t, VT_INT);
// 以圆为轮廓,矩形的草图为路径曲线进行扫掠去料
pDoc->Models->Item(1)->GetSweptCutouts()->Add(
1,
TraceCurves,
TraceCurveTypes,
1,
CrossSections,
CrossSectionTypes,
Origins,
0,
FeaturePropertyConstants::igLeft,
FeaturePropertyConstants::igNone,
0.0,
NULL,
FeaturePropertyConstants::igNone,
0.0,
NULL
);
} 示例效果:
二、螺旋除料
使用螺旋除料可以沿着螺旋路径扫掠横截面,以此创建除料,主要用到的方法为HelixCutouts下的一组Add方法,下面以添加有限范围除料即AddFinite为例进行演示。其原型为
HelixCutoutPtr AddFinite(struct RefAxis* HelixAxis, enum FeaturePropertyConstants AxisStart,
long NumCrossSections, SAFEARRAY** CrossSectionArray, enum FeaturePropertyConstants ProfileSide,
double Height, double Pitch, double NumberOfTurns, enum FeaturePropertyConstants HelixDir,
const _variant_t& TaperAngle = vtMissing, const _variant_t& TaperSide = vtMissing,
const _variant_t& EndPitch = vtMissing, const _variant_t& PitchRatio = vtMissing,
const _variant_t& TaperStartRadius = vtMissing, const _variant_t& TaperEndRadius = vtMissing);void DemoAddHelixCutouts()
{
namespace pt = TGPart;
namespace gm = TGGeometry;
namespace as = TGAssembly;
namespace fwp = TGFrameworkSupport;
// 建一个圆柱体用于演示除料
pt::PartDocumentPtr pPartDoc = SEAPP->GetActiveDocument();
assert(pPartDoc);
pt::ProfilePtr pProfile = pPartDoc->GetSketches()->AddByPlane(pPartDoc->GetRefPlanes()->Item(1l))->GetProfile();
pProfile->GetCircles2d()->AddByCenterRadius(0, 0, 0.05);// 中心位于原点,半径0.5
ATL::CComSafeArray<IDispatch*> aProfiles(1);
aProfiles.SetAt(0, pProfile);
pt::ModelPtr pModel = pPartDoc->GetModels()->AddFiniteExtrudedProtrusion(aProfiles.GetCount(), aProfiles.GetSafeArrayPtr(), pt::igRight, 0.3);
// XOZ平面构造螺旋轴和截面
pt::ProfilePtr pHelixProfile = pPartDoc->GetSketches()->AddByPlane(pPartDoc->GetRefPlanes()->Item(3l))->GetProfile();
fwp::Circle2dPtr pCir2d = pHelixProfile->GetCircles2d()->AddByCenterRadius(0.05, 0, 0.01);
fwp::Line2dPtr pSeg = pHelixProfile->GetLines2d()->AddBy2Points(0, 0, 0, 0.3);
pt::RefAxisPtr pRotateAxis = pHelixProfile->SetAxisOfRevolution(pSeg);
// 进行螺旋除料
ATL::CComSafeArray<IDispatch*> crossSectionArray(1);
crossSectionArray.SetAt(0, pHelixProfile);
pModel->GetHelixCutouts()->AddFinite(pRotateAxis,// 螺旋轴
pt::igStart,// pt::igStart表示从轴定义的线段起点向终点向螺旋,pt::igEnd表示从轴定义的线段终点向起点螺旋
1, crossSectionArray.GetSafeArrayPtr(), // 传入横截面
pt::igRight, // 为未来开放路径预留,暂时无用,pt::igRight和pt::igLeft为有效值
0.3,// 螺高,暂时无用
0.1, // 螺距
3, // 螺数
pt::igLeft// pt::igRight右旋,pt::igLeft左旋
);
} 上述代码演示了这个方法使用的一般过程,运行效果如下。
可以看到一般情况下,我们只需传入旋转轴、螺旋截面、旋转方向、螺距、螺高、圈数等参数就可以创建一个螺旋除料特征,如果想使螺距、螺径变化,则还可以输入更多的参数。
三、法向除料
法向除料与法向拉伸类似,是通过拉伸位于零件面上的封闭曲线构造垂直于该面的除料。使用文本轮廓或其他草图元素在非平整面上构造特征时,此命令非常有用。创建一个法向除料特征,可使用NormalToFaceCutouts::Add方法,除返回值外,此方向原型与法向拉伸NormalToFaceProtrusions::Add一致。
NormalToFaceCutoutPtr Add(long NumberOfInputCurves, // 闭合轮廓的拓扑边集个数
const _variant_t& InputCurvesSetArray, //闭合轮廓的拓扑边集
enum FeaturePropertyConstants LongestCurveSide, // 拉伸方向(在除料时 pt::igLeft pt::igRight未发现区别)
double offsetDistance, // 除料厚度
enum FeaturePropertyConstants FaceContainment // pt::igFacesTouchingCurvesOnly 仅修改与曲线接触的面
// pt::igAllFaces 修改曲线图内的所有面
)void DemoAddNormalCutouts()
{
namespace pt = TGPart;
namespace gm = TGGeometry;
namespace as = TGAssembly;
namespace fwp = TGFrameworkSupport;
pt::PartDocumentPtr pPartDoc = SEAPP->GetActiveDocument();
assert(pPartDoc);
// 首先创建一个圆柱
pt::RefPlanePtr pYOZPlane = pPartDoc->GetRefPlanes()->Item(2l);
pt::ProfilePtr pProfile = pPartDoc->GetSketches()->AddByPlane(pYOZPlane)->GetProfile();
pProfile->GetCircles2d()->AddByCenterRadius(0.0, 0.0, 0.2);
ATL::CComSafeArray<IDispatch*> aProfiles(1);
aProfiles.SetAt(0, pProfile);
pt::ModelPtr pCylinderModel = pPartDoc->GetModels()->AddFiniteExtrudedProtrusion(1, aProfiles.GetSafeArrayPtr(),
pt::igSymmetric, 0.6);
// 在一个新的草图上创建一个圆
pt::RefPlanePtr pXOYPlane = pPartDoc->GetRefPlanes()->Item(1l);
pt::SketchPtr pSketch = pPartDoc->GetSketches()->AddByPlane(pXOYPlane);
pt::ProfilePtr pProfileScd = pSketch->GetProfile();
pProfileScd->GetCircles2d()->AddByCenterRadius(0.0, 0.0, 0.05);
// 投影圆所在草图到圆柱面上
gm::BodyPtr pBody = pCylinderModel->GetBody();
gm::FacesPtr pFaces = pBody->GetFaces(gm::igQueryCylinder);
assert(pFaces->GetCount() > 0);
gm::FacePtr pFace = pFaces->Item(1l);
long faceCnt = pFaces->GetCount();
pt::ProjectCurvesPtr pProjCvs = pPartDoc->GetConstructions()->GetProjectCurves();
pt::RefPlanePtr pDestPlane = pPartDoc->GetRefPlanes()->AddParallelByDistance(pXOYPlane, 0.2, pt::igNormalSide);
pt::ProjectCurvePtr pProjCv = pProjCvs->Add(pSketch, pFace, pDestPlane, pt::igRight, pt::igProjectOptionProject);
// 法向拉伸
gm::EdgesPtr pProjEdges = pProjCv->GetEdges(gm::igQueryAll);
long projEdgeCnt = pProjEdges->GetCount();
ATL::CComSafeArray<IDispatch*> aEdges(projEdgeCnt);
for (long i = 0; i < projEdgeCnt; i++)
{
aEdges.SetAt(i, pProjEdges->Item(i + 1));
}
_variant_t varEdges;
varEdges.parray = aEdges.Detach();
varEdges.vt = VT_ARRAY | VT_DISPATCH;
pCylinderModel->GetNormalToFaceCutouts()->Add(projEdgeCnt,// 传入的eges个数
varEdges, // edges
pt::igLeft, // pt::igLeft 向外拉伸(加料) pt::igRight 向内拉伸(除料)
0.01,// 除料厚度
pt::igFacesTouchingCurvesOnly // pt::igFacesTouchingCurvesOnly 仅修改与曲线接触的面
// pt::igAllFaces 修改曲线图内的所有面
);
} 上述代码演示了使用此方向创建一个法向除料的一般过程,运行效果如下。
页:
[1]