mildcat 发表于 2014-9-27 21:01:56

UG NX二次开发源码:创建孔加工程序



UG NX二次开发源码:创建孔加工程序

#include <uf.h>
#include <uf_ui.h>
#include <uf_defs.h>
#include <uf_obj.h>
#include <uf_object_types.h>

#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/CAM_ArcOutputTypeCiBuilder.hxx>
#include <NXOpen/CAM_CAMObject.hxx>
#include <NXOpen/CAM_CAMSetup.hxx>
#include <NXOpen/CAM_CutParameters.hxx>
#include <NXOpen/CAM_CutVolumeGeom.hxx>
#include <NXOpen/CAM_CylinderMillingBuilder.hxx>
#include <NXOpen/CAM_CylinderMillingCutParameters.hxx>
#include <NXOpen/CAM_DisplayPaint.hxx>
#include <NXOpen/CAM_DisplayPath.hxx>
#include <NXOpen/CAM_DisplaySilhouette.hxx>
#include <NXOpen/CAM_DisplayTool.hxx>
#include <NXOpen/CAM_FeatureGeometry.hxx>
#include <NXOpen/CAM_FeedsBuilder.hxx>
#include <NXOpen/CAM_FeedsOptimizationData.hxx>
#include <NXOpen/CAM_GeometryCiBuilder.hxx>
#include <NXOpen/CAM_HoleBossGeom.hxx>
#include <NXOpen/CAM_HoleBossSet.hxx>
#include <NXOpen/CAM_HoleBossSetList.hxx>
#include <NXOpen/CAM_InferredDouble.hxx>
#include <NXOpen/CAM_Inheritable2dLength.hxx>
#include <NXOpen/CAM_InheritableDoubleBuilder.hxx>
#include <NXOpen/CAM_InheritableFeedBuilder.hxx>
#include <NXOpen/CAM_InheritableFeedModeBuilder.hxx>
#include <NXOpen/CAM_InheritableIntBuilder.hxx>
#include <NXOpen/CAM_InheritableTextBuilder.hxx>
#include <NXOpen/CAM_InheritableToolDepBuilder.hxx>
#include <NXOpen/CAM_Method.hxx>
#include <NXOpen/CAM_MultiBladeBaseGeometry.hxx>
#include <NXOpen/CAM_MultiBladeSplittersGeometry.hxx>
#include <NXOpen/CAM_MultipleStepoverBuilder.hxx>
#include <NXOpen/CAM_NCGroup.hxx>
#include <NXOpen/CAM_NCGroupCollection.hxx>
#include <NXOpen/CAM_NcmAvoidancePointBuilder.hxx>
#include <NXOpen/CAM_NcmClearanceBuilder.hxx>
#include <NXOpen/CAM_NcmHoleMachining.hxx>
#include <NXOpen/CAM_NcmHoleMachiningEngRet.hxx>
#include <NXOpen/CAM_NcmTransferBuilder.hxx>
#include <NXOpen/CAM_Operation.hxx>
#include <NXOpen/CAM_OperationBuilder.hxx>
#include <NXOpen/CAM_OperationCollection.hxx>
#include <NXOpen/CAM_OperationDisplayOptionsBuilder.hxx>
#include <NXOpen/CAM_ParamBuilder.hxx>
#include <NXOpen/CAM_PathDisplayColors.hxx>
#include <NXOpen/CAM_PostEventsCiBuilder.hxx>
#include <NXOpen/CAM_SplitterList.hxx>
#include <NXOpen/CAM_SpunOutlineGeom.hxx>
#include <NXOpen/CAM_StepoverBuilder.hxx>
#include <NXOpen/CAM_StockPerPassBuilder.hxx>
#include <NXOpen/CAM_ThreadedBossSetList.hxx>
#include <NXOpen/CAM_ThreadedHoleSetList.hxx>
#include <NXOpen/CAM_Tool.hxx>
#include <NXOpen/CAM_ToolChangeCiBuilder.hxx>
#include <NXOpen/CAM_Ude.hxx>
#include <NXOpen/CAM_UdeList.hxx>
#include <NXOpen/CAM_UdeSet.hxx>
#include <NXOpen/CAM_VerticalPosition.hxx>
#include <NXOpen/Direction.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/Features_BodyFeature.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/ObjectList.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Plane.hxx>
#include <NXOpen/Point.hxx>
#include <NXOpen/SelectTaggedObject.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/SmartObject.hxx>
#include <NXOpen/TaggedObject.hxx>
using namespace NXOpen;

#include <stdarg.h>

static void ECHO(char *format, ...)
{
    char msg;
    va_list args;
    va_start(args, format);
    vsprintf(msg, format, args);
    va_end(args);
    UF_UI_open_listing_window();
    UF_UI_write_listing_window(msg);
    UF_print_syslog(msg, FALSE);
}

#define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))

static int report_error( char *file, int line, char *call, int irc)
{
    if (irc)
    {
      char err;

      UF_get_fail_message(irc, err);
      ECHO("*** ERROR code %d at line %d in %s:\n",
            irc, line, file);
      ECHO("+++ %s\n", err);
      ECHO("%s;\n", call);
    }

    return(irc);
}

static int mask_for_faces(UF_UI_selection_p_t select, void *type)
{
    UF_UI_mask_t
      mask = { UF_solid_type, 0, UF_UI_SEL_FEATURE_ANY_FACE };

    if (!UF_CALL(UF_UI_set_sel_mask(select,
            UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, 1, &mask)))
      return (UF_UI_SEL_SUCCESS);
    else
      return (UF_UI_SEL_FAILURE);
}

static tag_t select_a_face(char *prompt)
{
    int
      resp;
    double
      cp;
    tag_t
      object,
      view;

    UF_CALL(UF_UI_select_with_single_dialog("Select a face", prompt,
      UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, mask_for_faces, NULL, &resp,
      &object, cp, &view));

    if (resp == UF_UI_OBJECT_SELECTED || resp == UF_UI_OBJECT_SELECTED_BY_NAME)
    {
      UF_CALL(UF_DISP_set_highlight(object, FALSE));
      return object;
    }
    else return NULL_TAG;

}



extern "C" DllExport int ufusr_ask_unload()
{
    return (int)Session::LibraryUnloadOptionImmediately;
}


extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
{
    Session *theSession = Session::GetSession();
    UF_initialize();

    Part *workPart(theSession->Parts()->Work());
    Part *displayPart(theSession->Parts()->Display());
    // ----------------------------------------------
    //   Menu: Insert->Operation...
    // ----------------------------------------------
    // ----------------------------------------------
    //   Dialog Begin Create Operation
    // ----------------------------------------------
    Session::UndoMarkId markId1;
    markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible, "Create Operation");
   
    CAM::NCGroup *nCGroup1(dynamic_cast<CAM::NCGroup *>(workPart->CAMSetup()->CAMGroupCollection()->FindObject("PROGRAM")));
    CAM::Method *method1(dynamic_cast<CAM::Method *>(workPart->CAMSetup()->CAMGroupCollection()->FindObject("MILL_FINISH")));
    CAM::Tool *tool1(dynamic_cast<CAM::Tool *>(workPart->CAMSetup()->CAMGroupCollection()->FindObject("MILL")));
    CAM::FeatureGeometry *featureGeometry1(dynamic_cast<CAM::FeatureGeometry *>(workPart->CAMSetup()->CAMGroupCollection()->FindObject("WORKPIECE")));
    CAM::Operation *operation1;
    operation1 = workPart->CAMSetup()->CAMOperationCollection()->Create(nCGroup1, method1, tool1, featureGeometry1, "mill_planar", "HOLE_MILLING", CAM::OperationCollection::UseDefaultNameTrue, "HOLE_MILLING");
   
    Session::UndoMarkId markId2;
    markId2 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Start");
   
    CAM::CylinderMillingBuilder *cylinderMillingBuilder1;
    cylinderMillingBuilder1 = workPart->CAMSetup()->CAMOperationCollection()->CreateCylinderMillingBuilder(operation1);
   
    theSession->SetUndoMarkName(markId2, "Hole Milling Dialog");
   
    // ----------------------------------------------
    //   Dialog Begin Hole Milling
    // ----------------------------------------------
    CAM::HoleBossGeom *holeBossGeom1;
    holeBossGeom1 = cylinderMillingBuilder1->Geometry()->HoleBossGeom();
   
    Session::UndoMarkId markId3;
    markId3 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Start");
   
    // Important - setting Geom type to hole type
    holeBossGeom1->SetHoleBossGeomType(CAM::HoleBossGeom::HoleBossTypesHole);
   
    CAM::HoleBossSetList *holeBossSetList1;
    holeBossSetList1 = holeBossGeom1->HoleList();
   
    CAM::HoleBossSetList *holeBossSetList2;
    holeBossSetList2 = holeBossGeom1->BossList();
   
    CAM::ThreadedBossSetList *threadedBossSetList1;
    threadedBossSetList1 = holeBossGeom1->ThreadedBossList();
   
    CAM::ThreadedHoleSetList *threadedHoleSetList1;
    threadedHoleSetList1 = holeBossGeom1->ThreadedHoleList();
   
    theSession->SetUndoMarkName(markId3, "Hole or Boss Geometry Dialog");
   
    // ----------------------------------------------
    //   Dialog Begin Hole or Boss Geometry
    // ----------------------------------------------
    std::vector<NXObject *> entities1(1);
   
    // using UFUNC face selection

    tag_t theFaceTag = select_a_face("Pick a Face:");
    if(NULL_TAG == theFaceTag) return;

    Face *face1 = dynamic_cast<Face *>(NXObjectManager::Get(theFaceTag));


    entities1 = face1;
    NXObject *nullNXObject(NULL);
    CAM::HoleBossSet *holeBossSet1;
    holeBossSet1 = holeBossGeom1->CreateHoleBossBuilder(entities1, 0.0, 0.0, nullNXObject, 0);
   
    holeBossSetList1->Append(holeBossSet1);
   
    holeBossSet1->InferLogic(face1);
   
    Session::UndoMarkId markId4;
    markId4 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Hole or Boss Geometry");
   
    theSession->DeleteUndoMark(markId4, NULL);
   
    theSession->SetUndoMarkName(markId3, "Hole or Boss Geometry");
   
    theSession->DeleteUndoMark(markId3, NULL);
   
    Session::UndoMarkId markId5;
    markId5 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Hole Milling");
   
    NXObject *nXObject1;
    nXObject1 = cylinderMillingBuilder1->Commit();
   
    theSession->DeleteUndoMark(markId5, NULL);
   
    theSession->SetUndoMarkName(markId2, "Hole Milling");
   
    cylinderMillingBuilder1->Destroy();
   
    theSession->DeleteUndoMark(markId2, NULL);
   
    Session::UndoMarkId markId6;
    markId6 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Start");
   
    CAM::Operation *operation2(dynamic_cast<CAM::Operation *>(nXObject1));
    CAM::CylinderMillingBuilder *cylinderMillingBuilder2;
    cylinderMillingBuilder2 = workPart->CAMSetup()->CAMOperationCollection()->CreateCylinderMillingBuilder(operation2);
   
    theSession->SetUndoMarkName(markId6, "Hole Milling Dialog");
   
    // ----------------------------------------------
    //   Dialog Begin Hole Milling
    // ----------------------------------------------
    NXObject *nXObject2;
    nXObject2 = cylinderMillingBuilder2->Commit();
   
    std::vector<CAM::CAMObject *> objects1(1);
    CAM::Operation *operation3(dynamic_cast<CAM::Operation *>(nXObject2));
    objects1 = operation3;
    workPart->CAMSetup()->GenerateToolPath(objects1);
   
    Session::UndoMarkId markId7;
    markId7 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Hole Milling");
   
    theSession->DeleteUndoMark(markId7, NULL);
   
    theSession->SetUndoMarkName(markId6, "Hole Milling");
   
    cylinderMillingBuilder2->Destroy();
   
    theSession->DeleteUndoMark(markId6, NULL);
   
    Session::UndoMarkId markId8;
    markId8 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Start");
   
    CAM::CylinderMillingBuilder *cylinderMillingBuilder3;
    cylinderMillingBuilder3 = workPart->CAMSetup()->CAMOperationCollection()->CreateCylinderMillingBuilder(operation3);
   
    theSession->SetUndoMarkName(markId8, "Hole Milling Dialog");
   
    // ----------------------------------------------
    //   Dialog Begin Hole Milling
    // ----------------------------------------------
    Session::UndoMarkId markId9;
    markId9 = theSession->SetUndoMark(Session::MarkVisibilityInvisible, "Hole Milling");
   
    NXObject *nXObject3;
    nXObject3 = cylinderMillingBuilder3->Commit();
   
    theSession->DeleteUndoMark(markId9, NULL);
   
    theSession->SetUndoMarkName(markId8, "Hole Milling");
   
    cylinderMillingBuilder3->Destroy();
   
    theSession->DeleteUndoMark(markId8, NULL);
   
    UF_terminate();

----------------------------------------------
}

叶修 发表于 2014-9-28 22:03:40

学习了,看不懂啊

593232280 发表于 2019-4-25 18:45:53

学习了,高手
页: [1]
查看完整版本: UG NX二次开发源码:创建孔加工程序