首页 NXOpen C++ CAM二次开发:获取workpiece的所有几何Tag

NXOpen C++ CAM二次开发:获取workpiece的所有几何Tag

小白鼠 2019-03-27 10:36:34 0 5269
static void do_it(void)
{
    logical is_initialized;
    int object_count;
    int type;
    int subtype;
    tag_t *objects;

    char mesg[133];

    // Get the work part
    Session *theSession = Session::GetSession();
    Part *workPart(theSession->Parts()->Work());
    Part *displayPart(theSession->Parts()->Display());

    if (UF_CALL(UF_CAM_is_session_initialized(&is_initialized)) || (is_initialized == FALSE)) return;


    /* Get selected WORKPIECE object in ONT */
    UF_CALL(UF_UI_ONT_ask_selected_nodes( &object_count, &objects ));

    if (object_count == 1) {
        UF_CALL(UF_OBJ_ask_type_and_subtype (objects[0],&type,&subtype));

        if ((type == UF_machining_geometry_grp_type) && (subtype == UF_mill_geom_featr_subtype) ) {


            tag_t ObjectTag = NULL_TAG;


            // Get the object from the WORKPIECE tag
            CAM::FeatureGeometry *WP = (CAM::FeatureGeometry *)NXObjectManager::Get(objects[0]);


            CAM::MillGeomBuilder *millGeomBuilder1;
            millGeomBuilder1 = workPart->CAMSetup()->CAMGroupCollection()->CreateMillGeomBuilder(WP);


            // Get geometry set list
            CAM::GeometrySetList *pGeomSetList = millGeomBuilder1->PartGeometry()->GeometryList();
            std::vector<CAM::GeometrySet*> geomSetVec = pGeomSetList->GeTContents();


            // get geometry set
            std::vector<CAM::GeometrySet*>::iterator iter;
            for ( iter=geomSetVec.begin(); iter != geomSetVec.end(); iter++ )
            {
                CAM::GeometrySet* geomSet = dynamic_cast<CAM::GeometrySet*>(*iter);
                ScCollector *scCollector = geomSet->ScCollector();            
                std::vector<SelectionIntentRule*> rules;
                scCollector->GetRules(rules);


                //get selection rule
                std::vector<SelectionIntentRule*>::iterator ruleIter;
                for ( ruleIter=rules.begin(); ruleIter != rules.end(); ruleIter++ )
                {
                    SelectionIntentRule* selRule = dynamic_cast<SelectionIntentRule*>(*ruleIter);
                    NXOpen::SelectionIntentRule::RuleType ruleType = selRule->Type();
                    //there are different rule types, here just use body, face as sample
                    switch ( ruleType )
                    {
                        case NXOpen::SelectionIntentRule::RuleTypeBodyDumb:
                        {
                            //get bodies 
                            NXOpen::BodyDumbRule *bodyRule = dynamic_cast<BodyDumbRule*>(selRule);
                            std::vector<Body*> bodies;
                            bodyRule->GetData(bodies);
                            std::vector<Body*>::iterator bIter;
                            int index = 1;
                            for (bIter=bodies.begin(); bIter!=bodies.end(); bIter++ )
                            {
                                Body* body = dynamic_cast<Body*>(*bIter);
                                ObjectTag = body->GetTag();
                                sprintf(mesg,"the geometry object type is body, the index is %d and the tag is %d\n",index, ObjectTag);                                
                                WRITE(mesg);
                                body->Highlight();
                                index++;
                            }
                        }
                        break;


                        case NXOpen::SelectionIntentRule::RuleTypeFaceDumb:
                        {
                            //get faces 
                            NXOpen::FaceDumbRule *faceRule = dynamic_cast<FaceDumbRule*>(selRule);
                            std::vector<Face*> faces;
                            faceRule->GetData(faces);
                            std::vector<Face*>::iterator fIter;
                            int index = 1;
                            for (fIter=faces.begin(); fIter!=faces.end(); fIter++ )
                            {
                                Face* face = dynamic_cast<Face*>(*fIter);
                                ObjectTag = face->GetTag();
                                sprintf(mesg,"the geometry object type is face, the index is %d and the tag is %d\n",index, ObjectTag);                                
                                WRITE(mesg);
                                index++;
                            }
                        }
                        break;


                    default:
                        break;
                    }
                }
            }

            millGeomBuilder1->Destroy();


        } else {
            WRITE("type is not UF_machining_geometry_grp_type");
            WRITE(" or UF_mill_geom_featr_subtype");
        }
        UF_free(objects);

    } else {
        WRITE("The number of selected objects in ONT must be 1.");
    }

}

发表评论