6 Replies Latest reply on Apr 14, 2014 11:50 PM by Allen Liu

    How to get selected edge object in PartDoc?

    Allen Liu

      I met a problem about select edge object in partdoc. When it be selected, but it cannot get object.

      It's very odd.

       

      public class PartEventHandler : DocumentEventHandler
          {
              PartDoc doc;

              public PartEventHandler(ModelDoc2 modDoc, SwAddin addin)
                  : base(modDoc, addin)
              {
                  doc = (PartDoc)document;
              }

               

            override public bool AttachEventHandlers()
              {
                  doc.DestroyNotify += new DPartDocEvents_DestroyNotifyEventHandler(OnDestroy);
                  doc.NewSelectionNotify += new DPartDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);

                  ConnectModelViews();

                  return true;
              }

       

       

      public int OnNewSelection()
              {
                  //for test case //add date:2014/04/10
                  ModelDoc2 mDoc = (ModelDoc2)doc;
                  //SelectionMgr selectionMgr = null;
                  ISelectionMgr selectionMgr = (ISelectionMgr) mDoc.SelectionManager;


                  if (mDoc != null)
                  {
                      selectionMgr = mDoc.SelectionManager;
                      int selectedObjectCount = selectionMgr.GetSelectedObjectCount2(-1);

                      // needs elaborate SWITCH based on selection type.

                      if (selectedObjectCount == 1)
                      {
                          swSelectType_e selectionType = (swSelectType_e)selectionMgr.GetSelectedObjectType3(1, -1);

                          //retrieve feature expecting type with GetFeature()
                          //dynamic entity = selectionMgr.GetSelectedObject6(1, -1);
                        object entity = (object)selectionMgr.GetSelectedObject6(1, -1); //why cannot get edge and always return null?
                         
                         
                          string retName = mDoc.GetEntityName(entity);
                       
                                         

                          System.Windows.Forms.MessageBox.Show("Name is " + doc.GetEntityName(entity) + ".");

                         swtch (selectionType)
                          {
                              case swSelectType_e.swSelSKETCHPOINTS:
                                  System.Windows.Forms.MessageBox.Show("Sketch point.");
                                  break;
                              case swSelectType_e.swSelEDGES:
                                  System.Windows.Forms.MessageBox.Show("Edges");
                                  break;

                          }

                      }

                  }
                  return 0;
              }

            
          }

        • Re: How to get selected edge object in PartDoc?
          Simon Turner

          Your file seems corrupt - I cannot open it.

          • Re: How to get selected edge object in PartDoc?
            Amirtharaj Vk

            Hi Allen:

             

            I have written a code to get length of the selected edge. The code is below. It will help you to get selected edges.

             

            --------------------------------

            using SolidWorks.Interop.sldworks;

            using SolidWorks.Interop.swconst;

            using System.Runtime.InteropServices;

            using System;

             

            namespace Allen.csproj

            {

                public partial class SolidWorksMacro

                {       

                    ModelDoc2 mDoc;

                    ISelectionMgr selectionMgr;

                    swSelectType_e selectionType;

                    Entity swent;

                    Edge swedge;

                    Curve swcur;

                    Double edgelen;

                    CurveParamData  curparam;

                    int selcount;

             

                    public void Main()

                    {

                        mDoc = (ModelDoc2)swApp.ActiveDoc;

                        if (mDoc != null)

                        {

                            selectionMgr = (ISelectionMgr)mDoc.SelectionManager;

                            selectionType = (swSelectType_e)selectionMgr.GetSelectedObjectType3(1, -1);

                            if (selectionType == swSelectType_e.swSelEDGES)

                            {

                                swent =(Entity) selectionMgr.GetSelectedObject5 (1);

                                swedge =(Edge) swent;

                                swcur = (Curve)swedge.GetCurve();

                                curparam = (CurveParamData)swedge.GetCurveParams3();

                                edgelen = (Double)swcur.GetLength3(curparam.UMinValue,curparam.UMaxValue) * 1000;

                                System.Windows.Forms.MessageBox.Show("Selected Edge Length (mm): " + edgelen);

                            }

                        }

                    }

             

                    /// <summary>

                    ///  The SldWorks swApp variable is pre-assigned for you.

                    /// </summary>

                    public SldWorks swApp;

                }

            }

            --------------------------------

             

            Regards

            V K Amirtharaj

            EGS Computers India Pvt Ltd

            http://www.egsindia.com

            http://www.egs.co.in

            • Re: How to get selected edge object in PartDoc?
              Allen Liu

              I merge your code into my add-in. It's work very fine. Thanks a lot.