10 Replies Latest reply on Nov 29, 2017 8:54 AM by Alex Burnett

    How to make selection of a dimension using Solidworks API c#

    Girish Kumar

      Hi,

       

      I am changing the display dimension. I want to make the selection of changed dimension in a drawing file using Solid Works API C#

      Is there a way to select particular Dimension in a drawing file using Solid Works API C#

       

      Thanks,

      Girish

        • Re: How to make selection of a dimension using Solidworks API c#
          Manikandan Babu

          Hi Girish Kumar,

           

          Please try with below method it help you to select the dimension in drawing with respect to model display dimension.

           

          2018 SOLIDWORKS API Help - GetCorresponding Method (IView)

           

          Thanks

          Manikandan

            • Re: How to make selection of a dimension using Solidworks API c#
              Girish Kumar

              In Solid Works 2016, where can i make selection of dimension in drawing file.In this scenario i don't want to deal with part file.

              can you provide some help with few lines of code?

                • Re: How to make selection of a dimension using Solidworks API c#
                  Manikandan Babu

                  Hi Girish,

                   

                  Can you please provide the sample workflow.

                   

                  Thanks

                  Manikandan

                    • Re: How to make selection of a dimension using Solidworks API c#
                      Girish Kumar

                      Hi Mani,

                       

                      I want to make this dimension select through Solid works API C#.I want the selection of a particular dimension in a drawing file through code but couldn't overcome the problem.If its feasible to highlight the selection of dimension please let me know

                       

                      Thanks,

                      Girish

                       

                        • Re: How to make selection of a dimension using Solidworks API c#
                          Manikandan Babu

                          Hi Girish,

                           

                          Please go through the code,I hope it give the idea to solve.

                           

                          static void Main(string[] args)

                                  {

                                      SldWorks SwApp = (SldWorks)System.Runtime.InteropServices.Marshal.GetActiveObject("sldworks.Application");

                                      ModelDoc2 SwModel = (ModelDoc2)SwApp.ActiveDoc;

                                      DrawingDoc SwDraw =(DrawingDoc) SwModel;

                                      //Activating the drawing view and add the dimension.

                                      SwDraw.ActivateView("Drawing View1");

                                      bool boolstatus = SwModel.Extension.SelectByRay(0.548750670735953, 0.454527552410178, -2000.2475, 0, 0, -1, 1.33869713731939E-03, 1, false, 0, 0);

                                      boolstatus = SwModel.Extension.SelectByRay(0.537726106075676, 0.555323572161285, -2000.2475, 0, 0, -1, 1.33869713731939E-03, 1, true, 0, 0);

                                      DisplayDimension Mydim = SwModel.AddDimension2(0.619771002485532, 0.508862906821227, 0);

                                      SwModel.ClearSelection2(true);

                                      //Get the annotation and selct the dimension.

                                      Annotation Swann = Mydim.GetAnnotation();

                                      Swann.Select(true);

                                  }

                           

                           

                           

                           

                          Thanks

                          Manikandan

                          • Re: How to make selection of a dimension using Solidworks API c#
                            Alex Burnett

                            Girish Kumar wrote:

                             

                            Hi Mani,

                             

                            I want to make this dimension select through Solid works API C#.I want the selection of a particular dimension in a drawing file through code but couldn't overcome the problem.If its feasible to highlight the selection of dimension please let me know

                             

                            Thanks,

                            Girish

                             

                            What are your requirements for the unique dimensions that you want to select. Are you looking to select them if they are a specific length? Are you looking to select them by name? This would help us determine a better approach for finding your dimensions.

                              • Re: How to make selection of a dimension using Solidworks API c#
                                Girish Kumar

                                Hi Alex,

                                I am looking for selection of all the dimensions in drawing file by name using  solidworks api c#

                                  • Re: How to make selection of a dimension using Solidworks API c#
                                    Alex Burnett

                                    Girish Kumar wrote:

                                     

                                    Hi Alex,

                                    I am looking for selection of all the dimensions in drawing file by name using solidworks api c#

                                    I played around a bit with this so hopefully this works for you as well. It selected all DisplayDimension objects in all views on the current sheet.

                                     

                                            private void SelectAllDimensions(ModelDoc2 modelIn)
                                            {
                                                ModelDoc2 swDraw = default(ModelDoc2);
                                                IDrawingDoc drawing = default(IDrawingDoc);
                                                IAnnotation swAnno = default(IAnnotation);
                                                View v = default(View);
                                                Sheet swSheet = default(Sheet);
                                                long annoType;
                                                double start = DateTime.Now.TimeOfDay.TotalMilliseconds;
                                                try
                                                {
                                                    // Get base model file for the drawing
                                                    swDraw = modelIn;
                                                    // Cast as drawing since we know it's a drawing already
                                                    drawing = (IDrawingDoc)swDraw;
                                    
                                    
                                                    // If editing template, exit to edit sheet mode
                                                    if (!drawing.GetEditSheet())
                                                        drawing.EditSheet();
                                    
                                    
                                                    // Set sheet object as the current sheet
                                                    swSheet = (Sheet)drawing.GetCurrentSheet();
                                    
                                    
                                                    // Set the view object to the first view on the sheet
                                                    v = (View)drawing.GetFirstView();
                                    
                                    
                                                    // Loop through all drawing views
                                                    while (v != null)
                                                    {
                                                        // Get annotation for current view
                                                        swAnno = v.GetFirstAnnotation3();
                                    
                                    
                                                        // Loop through all annotations in this view
                                                        while (swAnno != null)
                                                        {
                                                            // Get annotation type and set layer based on type
                                                            annoType = swAnno.GetType();
                                    
                                    
                                                            // For all objects of DisplayDimension type, do the following
                                                            if (annoType == (long)swAnnotationType_e.swDisplayDimension)
                                                            {
                                                                // Select the DisplayDimension object and append to selection (true = allowing multiple selections)
                                                                swAnno.Select(true);
                                                            }
                                    
                                    
                                                            // Set annotation to next before the while loop restarts
                                                            swAnno = swAnno.GetNext3();
                                                        }
                                                        // set the view to the next view. this will return null if there are no more
                                                        v = (View)v.GetNextView();
                                                    }
                                                }
                                                catch (Exception e)
                                                {
                                                    Trace.WriteLine(e.Message + " " + e.Data);
                                                    MessageBox.Show("Select All Dimensions Error", e.Message + System.Environment.NewLine + e.Data, MessageBoxButton.OK, MessageBoxImage.Error);
                                                }
                                                finally
                                                {
                                                    // Let go of all references to solidworks objects
                                                    swDraw = null;
                                                    swAnno = null;
                                                    drawing = null;
                                                    v = null;
                                                    swSheet = null;
                                                    GC.Collect();
                                    
                                    
                                                    // Print duration for debugger
                                                    double finish = DateTime.Now.TimeOfDay.TotalMilliseconds;
                                                    Trace.WriteLine(string.Format("Select All Dimensions Duration: {0:#.00} seconds", (finish - start) / 1000));
                                                }
                                            }