单个删除面

// NX 8.5.3.3
// Journal created by Administrator on Mon Apr 20 11:21:24 2020 中国标准时间
//适用用多个体循环单个删除
#include "C:\\all_uf.h"
#include <uf_defs.h>
#include <NXOpen/NXException.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/BasePart.hxx>
#include <NXOpen/Builder.hxx>
#include <NXOpen/Expression.hxx>
#include <NXOpen/ExpressionCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/FaceDumbRule.hxx>
#include <NXOpen/Features_Brep.hxx>
#include <NXOpen/Features_DeleteFaceBuilder.hxx>
#include <NXOpen/Features_FeatureCollection.hxx>
#include <NXOpen/NXObject.hxx>
#include <NXOpen/Part.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Plane.hxx>
#include <NXOpen/PlaneCollection.hxx>
#include <NXOpen/Preferences_PartModeling.hxx>
#include <NXOpen/Preferences_PartPreferences.hxx>
#include <NXOpen/Preferences_PartPreferences.hxx>
#include <NXOpen/Preferences_PartVisualizationLine.hxx>
#include <NXOpen/ScCollector.hxx>
#include <NXOpen/ScRuleFactory.hxx>
#include <NXOpen/SelectISurface.hxx>
#include <NXOpen/SelectObject.hxx>
#include <NXOpen/SelectionIntentRule.hxx>
#include <NXOpen/Session.hxx>
#include <NXOpen/TaggedObject.hxx>
#include <NXOpen/Unit.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/FaceTangentRule.hxx>
#include <NXOpen/NXObjectManager.hxx>
#include <uf_defs.h>
#include <uf_ui_types.h>
#include <iostream>
#include <NXOpen/Session.hxx>
#include <NXOpen/UI.hxx>
#include <NXOpen/NXMessageBox.hxx>
#include <NXOpen/Callback.hxx>
#include <NXOpen/NXException.hxx>
#include <NXOpen/BlockStyler_UIBlock.hxx>
#include <NXOpen/BlockStyler_BlockDialog.hxx>
#include <NXOpen/BlockStyler_PropertyList.hxx>
 #include <NXOpen/BlockStyler_Group.hxx>
#include <NXOpen/BlockStyler_FaceCollector.hxx>
#include <uf.h>
 #include <uf_obj.h>
#include <NXOpen/NXObjectManager.hxx>
#include <NXOpen/PartCollection.hxx>
#include <NXOpen/Face.hxx>
#include <NXOpen/FaceTangentRule.hxx>
#include <NXOpen/ScCollectorCollection.hxx>
#include <NXOpen/ScRuleFactory.hxx>
using namespace NXOpen;

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

static int select_filter_proc_fn(tag_t object, int type[1], void* user_data, UF_UI_selection_p_t select)
{
    //uf_list_p_t  edge_list;
    //int edge_count = 0, Type = 0;
    //double Point[3];
    //double Dir[3];
    //double Box[6];
    //double Radius[3];
    //double RadData[3];
    //int NormDir, vertex_count=0;
    //UF_MODL_ask_face_data(object, &Type, Point, Dir, Box, Radius, RadData, &NormDir);
    //UF_MODL_ask_face_edges(object, &edge_list);
    //UF_MODL_ask_list_count(edge_list, &edge_count);
    //
    //    tag_t edge_tag = NULL_TAG;
    //    UF_MODL_ask_list_item(edge_list, 1, &edge_tag);
    //    double point1[3];
    //    double point2[3];
    //    UF_MODL_ask_edge_verts(edge_tag, point1, point2, &vertex_count);
    //

    if (object == NULL)
    {
        return UF_UI_SEL_REJECT;
    }
    //else if (object&&edge_count == 2 && Type == UF_cone_subtype && vertex_count <= 0)
    else if (object)
    {
        return UF_UI_SEL_ACCEPT;
    }
}

static int init_proc(UF_UI_selection_p_t select, void* user_data)
{
    int num_triples = 1;//可选类型的数量
    
    UF_UI_mask_t mask_triples[] =
    { UF_cylinder_subtype, UF_UI_SEL_NOT_A_FEATURE };//可选对象类型
    UF_UI_set_sel_mask(select, UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, num_triples, mask_triples);
    if ((UF_UI_set_sel_procs(select, select_filter_proc_fn, NULL, user_data)) == 0)
    {
        return UF_UI_SEL_SUCCESS;
    }
    else
    {
        return UF_UI_SEL_FAILURE;
    }
}

//以下是封装函数
static void DeleteFaces(tag_t faces)
{
    Session *theSession = Session::GetSession();
    Part *workPart(theSession->Parts()->Work());    
    Features::Feature *nullFeatures_Feature(NULL);
    Features::DeleteFaceBuilder *deleteFaceB;
    std::vector<Face *> face1;
    deleteFaceB = workPart->Features()->CreateDeleteFaceBuilder(nullFeatures_Feature);    
    FaceDumbRule *faceDumbRule1;
    NXOpen::Face *face2(dynamic_cast<NXOpen::Face *>(NXOpen::NXObjectManager::Get(faces)));
    face1.push_back(face2);
    faceDumbRule1 = workPart->ScRuleFactory()->CreateRuleFaceDumb(face1);
    std::vector<SelectionIntentRule *> rules1(1);
    rules1[0] = faceDumbRule1;
    deleteFaceB->FaceCollector()->ReplaceRules(rules1, false);
    NXObject *nXObject1;
    nXObject1 = deleteFaceB->Commit();
    deleteFaceB->Destroy();
}

extern "C" DllExport void ufusr(char *param, int *retCode, int paramLen)
{
    UF_initialize();
    char sCue[] = "象选择对话框";
    char sTitle[] = "象选择对话框";
    int iScope = UF_UI_SEL_SCOPE_NO_CHANGE;
    int iResponse;
    tag_t *tObject;
    int count = 0;
    UF_UI_select_with_class_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &count, &tObject);
    if (count)
    {
        
        for (int i = 0; i < count; i++)
        {
            DeleteFaces(tObject[i]);
        }
        
    }
    else
    {
        return;
    }
            
}
  1 //Delete_Face D:\Microsoft Visual Studio\2017\Community\Common7\IDE\VC\VCWizards\NX10_NXOpenCPP\templates\1033\root.txt
  2 //适用于单个体多个要删除的面
  3 // Mandatory UF Includes
  4 
  5 
  6 // Internal Includes
  7 #include <NXOpen/ListingWindow.hxx>
  8 #include <NXOpen/NXMessageBox.hxx>
  9 #include <NXOpen/UI.hxx>
 10 #include "C:\\all_uf.h"
 11 #include <uf_defs.h>
 12 #include <NXOpen/NXException.hxx>
 13 #include <NXOpen/Session.hxx>
 14 #include <NXOpen/BasePart.hxx>
 15 #include <NXOpen/Builder.hxx>
 16 #include <NXOpen/Expression.hxx>
 17 #include <NXOpen/ExpressionCollection.hxx>
 18 #include <NXOpen/Face.hxx>
 19 #include <NXOpen/FaceDumbRule.hxx>
 20 #include <NXOpen/Features_Brep.hxx>
 21 #include <NXOpen/Features_DeleteFaceBuilder.hxx>
 22 #include <NXOpen/Features_FeatureCollection.hxx>
 23 #include <NXOpen/NXObject.hxx>
 24 #include <NXOpen/Part.hxx>
 25 #include <NXOpen/PartCollection.hxx>
 26 #include <NXOpen/Plane.hxx>
 27 #include <NXOpen/PlaneCollection.hxx>
 28 #include <NXOpen/Preferences_PartModeling.hxx>
 29 #include <NXOpen/Preferences_PartPreferences.hxx>
 30 #include <NXOpen/Preferences_PartPreferences.hxx>
 31 #include <NXOpen/Preferences_PartVisualizationLine.hxx>
 32 #include <NXOpen/ScCollector.hxx>
 33 #include <NXOpen/ScRuleFactory.hxx>
 34 #include <NXOpen/SelectISurface.hxx>
 35 #include <NXOpen/SelectObject.hxx>
 36 #include <NXOpen/SelectionIntentRule.hxx>
 37 #include <NXOpen/Session.hxx>
 38 #include <NXOpen/TaggedObject.hxx>
 39 #include <NXOpen/Unit.hxx>
 40 #include <NXOpen/Face.hxx>
 41 #include <NXOpen/FaceTangentRule.hxx>
 42 #include <NXOpen/NXObjectManager.hxx>
 43 #include <iostream>
 44 #include <NXOpen/Session.hxx>
 45 #include <NXOpen/UI.hxx>
 46 #include <NXOpen/NXMessageBox.hxx>
 47 #include <NXOpen/Callback.hxx>
 48 #include <NXOpen/NXException.hxx>
 49 #include <NXOpen/BlockStyler_UIBlock.hxx>
 50 #include <NXOpen/BlockStyler_BlockDialog.hxx>
 51 #include <NXOpen/BlockStyler_PropertyList.hxx>
 52 #include <NXOpen/BlockStyler_Group.hxx>
 53 #include <NXOpen/BlockStyler_FaceCollector.hxx>
 54 #include <uf.h>
 55 #include <uf_obj.h>
 56 #include <uf_ui.h>
 57 #include <NXOpen/NXObjectManager.hxx>
 58 #include <NXOpen/PartCollection.hxx>
 59 #include <NXOpen/Face.hxx>
 60 #include <NXOpen/FaceTangentRule.hxx>
 61 #include <NXOpen/ScCollectorCollection.hxx>
 62 #include <NXOpen/ScRuleFactory.hxx>
 63 
 64 // Std C++ Includes
 65 #include <iostream>
 66 #include <sstream>
 67 
 68 using namespace NXOpen;
 69 using std::string;
 70 using std::exception;
 71 using std::stringstream;
 72 using std::endl;
 73 using std::cout;
 74 using std::cerr;
 75 
 76 //加入UF_CALL2020-4-8
 77 #define UF_CALL(X) (report_error( __FILE__, __LINE__, #X, (X)))
 78 
 79 static int report_error( char *file, int line, char *call, int irc )
 80 {
 81     if (irc)
 82     {
 83         char err[133], msg[133];
 84         sprintf(msg, "*** ERROR code %d at line %d in %s:\n+++ ",
 85             irc, line, file);
 86         UF_get_fail_message(irc, err);
 87         UF_print_syslog(msg, FALSE);
 88         UF_print_syslog(err, FALSE);
 89         UF_print_syslog("\n", FALSE);
 90         UF_print_syslog(call, FALSE);
 91         UF_print_syslog(";\n", FALSE);
 92 
 93         if (!UF_UI_open_listing_window())
 94         {
 95             UF_UI_write_listing_window(msg);
 96             UF_UI_write_listing_window(err);
 97             UF_UI_write_listing_window("\n");
 98             UF_UI_write_listing_window(call);
 99             UF_UI_write_listing_window(";\n");
100         }
101     }
102     return(irc);
103 }
104 
105 //------------------------------------------------------------------------------
106 
107 
108 
109 
110 //------------------------------------------------------------------------------
111 // NXOpen c++ test class 
112 //------------------------------------------------------------------------------
113 class Del_face
114 {
115     // class members
116 public:
117     static Session *theSession;
118     static UI *theUI;
119 
120     Del_face();
121     ~Del_face();
122 
123     void do_it();
124     void print(const NXString &);
125     void print(const string &);
126     void print(const char*);
127 
128 private:
129     Part *workPart, *displayPart;
130     NXMessageBox *mb;
131     ListingWindow *lw;
132 };
133 
134 //------------------------------------------------------------------------------
135 // Initialize static variables
136 //------------------------------------------------------------------------------
137 Session *(Del_face::theSession) = NULL;
138 UI *(Del_face::theUI) = NULL;
139 
140 //------------------------------------------------------------------------------
141 // Constructor 
142 //------------------------------------------------------------------------------
143 Del_face::Del_face()
144 {
145 
146     // Initialize the NX Open C++ API environment
147     Del_face::theSession = NXOpen::Session::GetSession();
148     Del_face::theUI = UI::GetUI();
149     mb = theUI->NXMessageBox();
150     lw = theSession->ListingWindow();
151 
152     workPart = theSession->Parts()->Work();
153     displayPart = theSession->Parts()->Display();
154     
155 }
156 
157 //------------------------------------------------------------------------------
158 // Destructor
159 //------------------------------------------------------------------------------
160 Del_face::~Del_face()
161 {
162 }
163 
164 //------------------------------------------------------------------------------
165 // Print string to listing window or stdout
166 //------------------------------------------------------------------------------
167 void Del_face::print(const NXString &msg)
168 {
169     if(! lw->IsOpen() ) lw->Open();
170     lw->WriteLine(msg);
171 }
172 void Del_face::print(const string &msg)
173 {
174     if(! lw->IsOpen() ) lw->Open();
175     lw->WriteLine(msg);
176 }
177 void Del_face::print(const char * msg)
178 {
179     if(! lw->IsOpen() ) lw->Open();
180     lw->WriteLine(msg);
181 }
182 
183 
184 static int select_filter_proc_fn(tag_t object, int type[1], void* user_data, UF_UI_selection_p_t select)
185 {
186     uf_list_p_t  edge_list;
187     int edge_count = 0, Type = 0;
188     double Point[3];
189     double Dir[3];
190     double Box[6];
191     double Radius[3];
192     double RadData[3];
193     int NormDir, vertex_count = 0;
194     UF_MODL_ask_face_data(object, &Type, Point, Dir, Box, Radius, RadData, &NormDir);
195     UF_MODL_ask_face_edges(object, &edge_list);
196     UF_MODL_ask_list_count(edge_list, &edge_count);
197     tag_t edge_tag = NULL_TAG;
198     UF_MODL_ask_list_item(edge_list, 0, &edge_tag);
199     double point1[3];
200     double point2[3];
201     UF_MODL_ask_edge_verts(edge_tag, point1, point2, &vertex_count);
202     if (object == NULL)
203     {
204         return UF_UI_SEL_REJECT;
205     }
206     else if (object&&edge_count == 2 && Type == UF_cone_subtype && vertex_count == 0)
207     {
208         return UF_UI_SEL_ACCEPT;
209     }
210 }
211 
212 static int init_proc(UF_UI_selection_p_t select, void* user_data)
213 {
214     int num_triples = 1;//可选类型的数量
215 
216     UF_UI_mask_t mask_triples[] =
217     { UF_face_type, UF_cone_subtype, UF_UI_SEL_NOT_A_FEATURE };//可选对象类型
218     UF_UI_set_sel_mask(select, UF_UI_SEL_MASK_CLEAR_AND_ENABLE_SPECIFIC, num_triples, mask_triples);
219     if ((UF_UI_set_sel_procs(select, select_filter_proc_fn, NULL, user_data)) == 0)
220     {
221         return UF_UI_SEL_SUCCESS;
222     }
223     else
224     {
225         return UF_UI_SEL_FAILURE;
226     }
227 }
228 
229 
230 //以下是封装函数
231 static void DeleteFaces(std::vector<Face *> faces)
232 {
233     Session *theSession = Session::GetSession();
234     Part *workPart(theSession->Parts()->Work());
235     Features::Feature *nullFeatures_Feature(NULL);
236     Features::DeleteFaceBuilder *deleteFaceB;
237     deleteFaceB = workPart->Features()->CreateDeleteFaceBuilder(nullFeatures_Feature);
238     FaceDumbRule *faceDumbRule1;
239     faceDumbRule1 = workPart->ScRuleFactory()->CreateRuleFaceDumb(faces);
240     std::vector<SelectionIntentRule *> rules1(1);
241     rules1[0] = faceDumbRule1;
242     deleteFaceB->FaceCollector()->ReplaceRules(rules1, false);
243     NXObject *nXObject1;
244     nXObject1 = deleteFaceB->Commit();
245     deleteFaceB->Destroy();
246 }
247 
248 
249 //------------------------------------------------------------------------------
250 // Do something
251 //------------------------------------------------------------------------------
252 void Del_face::do_it()
253 {
254 
255     // TODO: add your code here
256     UF_initialize();
257     char sCue[] = "象选择对话框";
258     char sTitle[] = "象选择对话框";
259     int iScope = UF_UI_SEL_SCOPE_NO_CHANGE;
260     int iResponse;
261     tag_t *tObject;
262     int count = 0;
263     UF_UI_select_with_class_dialog(sCue, sTitle, iScope, init_proc, NULL, &iResponse, &count, &tObject);
264     if (count)
265     {
266         std::vector<Face *> deleteface;
267         for (int i = 0; i < count; i++)
268         {
269             NXOpen::Face *face1(dynamic_cast<NXOpen::Face *>(NXOpen::NXObjectManager::Get(tObject[i])));
270             deleteface.push_back(face1);
271         }
272         DeleteFaces(deleteface);
273     }
274     else
275     {
276         return;
277     }
278     UF_terminate();
279 
280     
281 }
282 
283 //------------------------------------------------------------------------------
284 // Entry point(s) for unmanaged internal NXOpen C/C++ programs
285 //------------------------------------------------------------------------------
286 //  Explicit Execution
287 extern "C" DllExport void ufusr( char *parm, int *returnCode, int rlen )
288 {
289     try
290     {
291         // Create NXOpen C++ class instance
292         Del_face *theDel_face;
293         theDel_face = new Del_face();
294         theDel_face->do_it();
295         delete theDel_face;
296     }
297     catch (const NXException& e1)
298     {
299         UI::GetUI()->NXMessageBox()->Show("NXException", NXOpen::NXMessageBox::DialogTypeError, e1.Message());
300     }
301     catch (const exception& e2)
302     {
303         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, e2.what());
304     }
305     catch (...)
306     {
307         UI::GetUI()->NXMessageBox()->Show("Exception", NXOpen::NXMessageBox::DialogTypeError, "Unknown Exception.");
308     }
309 }
310 
311 
312 //------------------------------------------------------------------------------
313 // Unload Handler
314 //------------------------------------------------------------------------------
315 extern "C" DllExport int ufusr_ask_unload()
316 {
317     return (int)NXOpen::Session::LibraryUnloadOptionImmediately;
318 }

猜你喜欢

转载自www.cnblogs.com/liuxiaoqing1/p/12771409.html