admin 发表于 2018-8-23 08:19:35

NX二次开发源码分享: NXOpen C++获取面的面积和周长



NX二次开发源码分享: NXOpen C++获取面的面积和周长





double MyClass::MeasureFaceArea(TaggedObject* face_tag)
{
    char str;
      
    Unit *unit1(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("SquareMilliMeter")));
    Unit *unit2(dynamic_cast<Unit *>(workPart->UnitCollection()->FindObject("MilliMeter")));

    //FaceCollector issue is fixed in NX9
    NXObject *nullNXObject(NULL);
    MeasureFaceBuilder *measureFaceBuilder1;
    measureFaceBuilder1 = workPart->MeasureManager()->CreateMeasureFaceBuilder(nullNXObject);
      
    Face *face1 = dynamic_cast<Face *>(face_tag);
    vector<Face *> faces1(1);
    faces1 = face1;
    FaceDumbRule *faceDumbRule1;
    faceDumbRule1 = workPart->ScRuleFactory()->CreateRuleFaceDumb(faces1);
   
    std::vector<SelectionIntentRule *> rules1(1);
    rules1 = faceDumbRule1;
    measureFaceBuilder1->FaceCollector()->ReplaceRules(rules1, false);
    NXOpen::NXObject *nxObjct1 = measureFaceBuilder1->Commit();

    NXOpen::ScCollector *scCollector1 = measureFaceBuilder1->FaceCollector();

    MeasureFaces *measureFaces1;
    measureFaces1 = workPart->MeasureManager()->NewFaceProperties(unit1, unit2, 0.99, scCollector1);

    double face_area = measureFaces1->Area();
    sprintf_s(str, sizeof(str), "Selected Face Area: = %f",face_area);
    print(str);
   
    double face_perimeter = measureFaces1->Perimeter();
    sprintf_s(str, sizeof(str), "Selected Face Perimeter: = %f",face_perimeter);
    print(str);

    delete measureFaces1;   
    delete faceDumbRule1;
    measureFaceBuilder1->Destroy();
    return face_area;
}

rkc_555 发表于 2018-9-11 21:23:07

这个方法我试过了,我8.5的版本好像不行,计算的面积为0,可以直接创建measureFaces对象,用另一个重载函数,只要两行代码就能计算出面积
页: [1]
查看完整版本: NX二次开发源码分享: NXOpen C++获取面的面积和周长