C# NX二次开发-使用底层对话框制作透明面功能

功能效果:

首先把界面画出来:

代码如下:

public class Inlet1
{

  static Session theSession = null;
  static Part theWork = null;
  static UI theUI = null;
  static UFSession theUf = null;

  public static Inlet1 theFaceTranslucency;
  private Dialog theDialog;
  private NXOpen.UIStyler.DialogItem changeDialog;
  private NXOpen.UIStyler.CollapsibleGroup changeCgBeg1;
  private NXOpen.UIStyler.RadioBox changeRadio1;
  private NXOpen.UIStyler.CollapsibleGroup changeCgBeg0;
  private NXOpen.UIStyler.IntegerScale changeScaleInt0;
  public static bool isDisposeCalled;


  #region " UI Styler Dialog Designer generator code " 
  //------------------------------------------------------------------------------
  // Constructor for NX Styler class
  //------------------------------------------------------------------------------
  public Inlet1()
  {
    try
    {
      theSession = Session.GetSession();
      theUI = UI.GetUI();
      theDialog = theUI.Styler.CreateStylerDialog("FaceTranslucency.dlg");
      InitializeUIStylerDialog();
      isDisposeCalled = false;

    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
  }



  //------------------------------------------------------------------------------
  // This method is required for UI styler dialog creation
  //------------------------------------------------------------------------------
  private void InitializeUIStylerDialog()
  {
    try
    {
      //------------------------------------------------------------------------------
      // The following code snippets initializes all the styler items and associate
      // respective callbacks. Attributes of the styler item can be accessed and
      // modified only after calling Show() or RegisterWithUiMenu().
      //------------------------------------------------------------------------------
      changeDialog = (NXOpen.UIStyler.DialogItem)theDialog.GetStylerItem("UF_STYLER_DIALOG_INDEX", Dialog.ItemType.DialogItem);
      changeDialog.AddConstructHandler(new NXOpen.UIStyler.DialogItem.Construct(Main), false);
      changeDialog.AddBackHandler(new NXOpen.UIStyler.DialogItem.Back(back_cb), false);

      changeCgBeg1 = (NXOpen.UIStyler.CollapsibleGroup)theDialog.GetStylerItem("CG_BEG_1", Dialog.ItemType.CollapsibleGroup);
      changeRadio1 = (NXOpen.UIStyler.RadioBox)theDialog.GetStylerItem("RADIO_1", Dialog.ItemType.RadioBox);
      changeCgBeg0 = (NXOpen.UIStyler.CollapsibleGroup)theDialog.GetStylerItem("CG_BEG_0", Dialog.ItemType.CollapsibleGroup);
      changeScaleInt0 = (NXOpen.UIStyler.IntegerScale)theDialog.GetStylerItem("SCALE_INT_0", Dialog.ItemType.IntegerScale);
      changeScaleInt0.AddValueChangedHandler(new NXOpen.UIStyler.IntegerScale.ValueChanged(TranslucencyValue), false);
    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
  }

  #endregion

  // -------------------------- MENUBAR HOOKUP HELP Example ----------------------
  // To launch this dialog from a NX menubar, you must follow the steps below:


  //      1)Add the following lines to your MenuScript file in order to
  //        associate a menu bar button with your dialog.  In this
  //        example, a cascade menu will be created and will be
  //        located just before the Help button on the main menubar.
  //        The button, FACETRANSLUCENCY_BTN is set up to launch your dialog and
  //        will be positioned as the first button on your pulldown menu.
  //        If you wish to add the button to an existing cascade, simply
  //        add the 3 lines between MENU LAUNCH_CASCADE and END_OF_MENU
  //        to your menuscript file.

  //        The MenuScript file requires an extension of ".men".
  //        Make sure that you add the extension to the file and place
  //        the file in your startup directory:
  //             $UGII_USER_DIR/startup or
  //             $UGII_SITE_DIR/startup or
  //             $UGII_VENDOR_DIR/startup directory

  //        Move the contents between the dashed lines to your Menuscript file.
  //        !-----------------------------------------------------------------------------
  //        VERSION 120
  // 
  //        EDIT UG_GATEWAY_MAIN_MENUBAR
  // 
  //        BEFORE UG_HELP
  //        CASCADE_BUTTON UISTYLER_DLG_CASCADE_BTN
  //        LABEL Dialog Launcher
  //        END_OF_BEFORE
  // 
  //        MENU UISTYLER_DLG_CASCADE_BTN
  //        BUTTON FACETRANSLUCENCY_BTN
  //        LABEL Display FaceTranslucency dialog
  //        ACTIONS FaceTranslucency.dlg
  //        END_OF_MENU
  //        !-----------------------------------------------------------------------------

  //      2)Issue a call to the function, RegisterWithUiMenu() from the Startup()
  //        as shown below.
  //        To use this call, remove the conditional definitions:
  //             #if MENUBAR_COMMENTED_OUT
  //             #endif

  //      3)Place your compiled and linked DLL in 
  //             $UGII_USER_DIR/startup or
  //             $UGII_SITE_DIR/startup or
  //             $UGII_VENDOR_DIR/startup directory.

  //       NOTE:  The shared libraries must contain the proper extension .dll
  //       to make ensure that it is recognized by the MenuScript.  If it does not
  //       have the proper extension, it will NOT be recognized by MenuScript.

  //       The action name you have provided in your MenuScript must correspond to
  //       to the dialog name provided in the action structure.  This MUST match inorder
  //       to bind your dlg file to your MenuScript button.

  //      4)Copy your UIStyler dialog file to the proper directory.
  //        All dialog files (.dlg) must be located in 
  //             $UGII_USER_DIR/application or
  //             $UGII_SITE_DIR/application or
  //             $UGII_VENDOR_DIR/application directory
  //------------------------------------------------------------------------------
#if MENUBAR_COMMENTED_OUT
    public static int Startup()
    {
        int retValue = 0;
        try
        {
            theFaceTranslucency = new FaceTranslucency();
            // The following method registers the dialog with a menu item
            theFaceTranslucency.RegisterWithUiMenu();

            retValue = 0;
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
        return retValue;
    } // Startup ends

    //------------------------------------------------------------------------------
    // The flag "isTopDialog", informs menubar about the dialog location.
    // This flag MUST match the resource set in the dialog.
    // DO NOT ASSUME that changing this field will update the location of the dialog.
    // Please use the UIStyler to indicate the position of the dialog.
    //------------------------------------------------------------------------------
    public void RegisterWithUiMenu()
    {
        try
        {
            bool isTopDialog = false;
            theDialog.RegisterWithUiMenu(isTopDialog);
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
    }


#endif // MENUBAR_COMMENTED_OUT


  // --------------- DIALOG CREATION FROM A CALLBACK HELP Example -----------------
  // If you wish to have this dialog displayed from the callback of another
  // UIStyler dialog, you should:


  //      1)Make sure that the callback of your UIStyler dialog is
  //        designated as a dialog building callback.
  //      2)Remove the conditional definitions:
  //             #if DISPLAY_FROM_CALLBACK
  //             #endif

  //      3)Your callback should issue a call to this method.


  // All dialog files must be located in
  //             $UGII_USER_DIR/application or
  //             $UGII_SITE_DIR/application or
  //             $UGII_VENDOR_DIR/application directory
  //------------------------------------------------------------------------------
#if DISPLAY_FROM_CALLBACK
    public static void <enter the name of your function>(ref NXOpen.UIStyler.DialogResponse response)
    {
        try
        {
            theFaceTranslucency = new FaceTranslucency();
            // The following method shows the dialog immediately
            response = theFaceTranslucency.Show();
            theFaceTranslucency.Dispose();
        }
        catch (NXOpen.NXException ex)
        {
            // ---- Enter your exception handling code here -----
            theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
        }
    }

#endif // DISPLAY_FROM_CALLBACK


  //---------------- DIALOG CREATION FROM A USER EXIT HELP Example ---------------
  // An example is shown below to create this dialog from a user exit.
  // All dialog files must be located in
  //             $UGII_USER_DIR/application or
  //             $UGII_SITE_DIR/application or
  //             $UGII_VENDOR_DIR/application directory


  //      1)Remove the conditional definitions:
  //             #if DISPLAY_FROM_USER_EXIT
  //             #endif

  //      2)Add a user exit to the method name below, for example, Main().
  //      3)Consider how your shared library will be unloaded.  Take a look
  //        at the method GetUnloadOption().
  //------------------------------------------------------------------------------

  public static void Main()
  {
    try
    {

      theSession = Session.GetSession();
      theWork = theSession.Parts.Work;
      theUI = UI.GetUI();
      theUf = UFSession.GetUFSession();

      NXOpen.UIStyler.DialogResponse response;
      theFaceTranslucency = new Inlet1();
      // For Journal play back uncomment calls for LockAccess and UnlockAccess
      // theUI.LockAccess()
      // The following method shows the dialog immediately
      response = theFaceTranslucency.Show();
      theFaceTranslucency.Dispose();
      // theUI.UnlockAccess()
    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
  } // User exit ends

  //------------------------------------------------------------------------------
  // This method specifies how a shared image is unloaded from memory
  // within NX. This method gives you the capability to unload an
  // internal NX Open application or user  exit from NX. Specify any
  // one of the three constants as a return value to determine the type
  // of unload to perform:


  //    Immediately : unload the library as soon as the automation program has completed
  //    Explicitly  : unload the library from the "Unload Shared Image" dialog
  //    AtTermination : unload the library when the NX session terminates


  // NOTE:  A program which associates NX Open applications with the menubar
  // MUST NOT use this option since it will UNLOAD your NX Open application image
  // from the menubar.
  //------------------------------------------------------------------------------
  public static int GetUnloadOption(string arg)
  {

    return System.Convert.ToInt32(Session.LibraryUnloadOption.Explicitly);
    // return System.Convert.ToInt32(Session.LibraryUnloadOption.AtTermination);
  }

  //------------------------------------------------------------------------------
  // Following method cleanup any housekeeping chores that may be needed.
  // This method is automatically called by NX.
  //------------------------------------------------------------------------------
  public static int UnloadLibrary(string arg)
  {
    try
    {
      if (theFaceTranslucency == null)
      {
        theFaceTranslucency.Dispose();
      }
    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
    return 0;
  }




  //------------------------------------------------------------------------------
  // This method shows the dialog on the screen
  //------------------------------------------------------------------------------
  public NXOpen.UIStyler.DialogResponse Show()
  {
    NXOpen.UIStyler.DialogResponse retValue = NXOpen.UIStyler.DialogResponse.Ok;
    try
    {
      retValue = theDialog.Show();
    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
    return retValue;
  }


  //------------------------------------------------------------------------------
  // Following method disposes all the class members
  //------------------------------------------------------------------------------
  public void Dispose()
  {
    try
    {
      if (isDisposeCalled == false)
      {
        changeDialog.Dispose();
        changeCgBeg1.Dispose();
        changeRadio1.Dispose();
        changeCgBeg0.Dispose();
        changeScaleInt0.Dispose();
        theDialog.Dispose();
      }
      isDisposeCalled = true;
    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
  }

  //------------------------------------------------------------------------------
  //------------------------- UIStyler Callback Functions ------------------------
  //------------------------------------------------------------------------------


  //------------------------------------------------------------------------------
  // Callback Name: Main
  // Following callback is associated with the "changeDialog" Styler item.
  // Input: eventObject - object of UIStyler.StylerEvent class
  //------------------------------------------------------------------------------
  public NXOpen.UIStyler.DialogState Main(NXOpen.UIStyler.StylerEvent eventObject)
  {
    try
    {


      dialogShown_cb(eventObject);


    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
    // Callback acknowledged, do not terminate dialog
    // A return value of NXOpen.UIStyler.DialogState.ExitDialog will not be accepted
    // for this callback type. You must continue dialog construction.
    return NXOpen.UIStyler.DialogState.ContinueDialog;
  }


  public DialogState dialogShown_cb(StylerEvent eventObject)
  {
    try
    {
      //----Enter your callback code here -----
      var masks = new List<Selection.MaskTriple>();
      var mask = new Selection.MaskTriple();
      mask.Type = UFConstants.UF_solid_type;           //实体
      mask.Subtype = UFConstants.UF_solid_face_subtype;      //面
      mask.SolidBodySubtype = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE;
      masks.Add(mask);

      var sh = changeDialog.GetSelectionHandle();
      theUI.SelectionManager.SetSelectionMask(sh, Selection.SelectionAction.ClearAndEnableSpecific, masks.ToArray());
      theUI.SelectionManager.SetTaggedObjectSelectionCallbacks(sh, FilterTaggedObject, TaggedObjectSelection);

    }
    catch (Exception ex)
    {
      //---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("Block Styler", NXMessageBox.DialogType.Error, ex.ToString());
    }
    return DialogState.ContinueDialog;

  }

  public int FilterTaggedObject(TaggedObject entity, Selection.MaskTriple entityMask, SelectionHandle selectHandle)
  {
    //theUf.Modl.AskFaceTopology(entity.Tag, out var topo_type);
    //if (topo_type != UFConstants.UF_MODL_CYLINDRICAL_TOPOLOGY)
    //{
    //    return 0;
    //}
    return 1;
  }

  public int TaggedObjectSelection(TaggedObject[] selectedObjects, TaggedObject[] deselectedObjects, SelectionHandle selectHandle)
  {

    var groups = selectedObjects.Select(x =>
    {
      theUf.Obj.AskTranslucency(x.Tag, out int translucency);
      return new { taggde = x, trans = translucency };
    }).GroupBy((x) => x.trans == 0).ToArray();

    foreach (var group in groups)
    {
      var disps = group.Select(x => x.taggde as DisplayableObject).ToArray();
      var dm = theSession.DisplayManager.NewDisplayModification();
      dm.ApplyToAllFaces = true;
      dm.ApplyToOwningParts = false;
      //dm.NewWidth = NXOpen.DisplayableObject.ObjectWidth.Four;
      if (changeRadio1.ItemValue == 0)
      {
        dm.NewTranslucency = group.Key ? changeScaleInt0.ItemValue : 0;
      }
      else if (changeRadio1.ItemValue == 1)
      {
        dm.NewTranslucency = changeScaleInt0.ItemValue;
      }
      else if (changeRadio1.ItemValue == 2)
      {
        dm.NewTranslucency = 0;
      }

      dm.Apply(disps);
      dm.Dispose();
    }


    theUI.SelectionManager.RemoveAllFromSelectionList(selectHandle, true);
    return 0;
  }


  //------------------------------------------------------------------------------
  // Callback Name: back_cb
  // Following callback is associated with the "changeDialog" Styler item.
  // Input: eventObject - object of UIStyler.StylerEvent class
  //------------------------------------------------------------------------------
  public NXOpen.UIStyler.DialogState back_cb(NXOpen.UIStyler.StylerEvent eventObject)
  {
    try
    {


      // ---- Enter your callback code here -----


    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
    // Callback acknowledged, terminate dialog
    // It is STRONGLY recommanded that you exit your
    // callback with NXOpen.UIStyler.DialogState.ExitDialog in a back callback
    // rather than NXOpen.UIStyler.DialogState.ContinueDialog
    return NXOpen.UIStyler.DialogState.ExitDialog;
  }

  //------------------------------------------------------------------------------
  // Callback Name: TranslucencyValue
  // Following callback is associated with the "changeScaleInt0" Styler item.
  // Input: eventObject - object of UIStyler.StylerEvent class
  //------------------------------------------------------------------------------
  public NXOpen.UIStyler.DialogState TranslucencyValue(NXOpen.UIStyler.StylerEvent eventObject)
  {
    try
    {


      // ---- Enter your callback code here -----


    }
    catch (NXOpen.NXException ex)
    {
      // ---- Enter your exception handling code here -----
      theUI.NXMessageBox.Show("UI Styler", NXMessageBox.DialogType.Error, ex.Message);
    }
    // Callback acknowledged, do not terminate dialog
    return NXOpen.UIStyler.DialogState.ContinueDialog;
    // or Callback acknowledged, terminate dialog.
    // return NXOpen.UIStyler.DialogState.ExitDialog;
  }

}

项目已上传: 

C#NX二次开发外挂透明面功能资源-CSDN文库

  • 17
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值