8 Replies Latest reply on Feb 13, 2018 5:38 AM by Korbi Anis

    Bounding circle of 2D sketch

    Korbi Anis

      Hi friends , Is it possible to genrale the bounding circle ( minimun circumscribed circle ) of 2D sketch using VBA ? Thank you

        • Re: Bounding circle of 2D sketch
          Ivana Kolin

          it is possible. You only need to find someone who translates it for you to VBA

          Smallest enclosing circle

            • Re: Bounding circle of 2D sketch
              Korbi Anis

              I succeded to create the 2D bounding box of a selected 2D sketch in VBA solidworks.But finding the bounding circle of a selected sketch is not too easy

                • Re: Bounding circle of 2D sketch
                  Ivana Kolin

                  no it is not easy, therefore I think that nobody has time to convert it for free.

                    • Re: Bounding circle of 2D sketch
                      Korbi Anis

                      Yes thats's right ! . Maybe I found the solution.I found a Matlab code that make it possible to get the bounding circle of a set of points in the plan ( X,Y) .By using VBA excel its possible to call the matlab script and excute the command directly vwith VBA.Just in VBA its necessary to call the script function to get the result.

                      this is the macro but its not for the bounding circle but for another simple function:

                      Sub CustomFunctionOneOutput()




                          'This macro executes the custom MATLAB function "TrapezoidArea" using the MATLAB COM Automation Server.

                          'After running the function the result is being further processed in order to remove unnecessary spaces and get the required value.


                          'The "TrapezoidArea" cusotm function is used for demonstration purposes.

                          'It calculates the area of trapezoid using the formula Area = 0.5*(LargeBase + SmallBase)*Height.


                          'It goes without saying that in order to use this macro you must have installed MATLAB at your computer...





                          'Declaring the necessary variables.

                          Dim LargeBase   As Double

                          Dim SmallBase   As Double

                          Dim Height      As Double

                          Dim Matlab      As Object

                          Dim mFilePath   As String

                          Dim Result      As String

                          Dim temp        As String


                          'Get the input values.

                          LargeBase = Sheet1.Range("O17").Value

                          SmallBase = Sheet1.Range("O18").Value

                          Height = Sheet1.Range("O19").Value


                          'Set the MATLAB object (the COM server).

                          On Error Resume Next

                          Set Matlab = CreateObject("matlab.application")

                          'In the case of error inform the user and exit the macro.

                          If Err.Number <> 0 Then

                              MsgBox "Could not open Matlab!", vbCritical, "Matlab Error"

                              Exit Sub

                          End If

                          On Error GoTo 0


                          'Specify the location of the m file that will be used. If the file is in a location different

                          'than the current workbook you can use the full path, for example:

                          'mFilePath = "C:\Users\Christos\Desktop"

                          mFilePath = ThisWorkbook.Path



                          'Load the m file in MATLAB.

                          Matlab.Execute ("cd('" & mFilePath & "\')")


                          'Execute the custom function.

                          Result = Matlab.Execute("TrapezoidArea(" & LargeBase & "," & SmallBase & "," & Height & ")")


                          'Remove the unnecessary spaces from the string Result.

                          temp = WorksheetFunction.Substitute(WorksheetFunction.Substitute(Result, Chr(10), ""), " ", "")


                          'Display the function result to the user.

                          MsgBox "Trapezoid Area = " & Right(temp, Len(temp) - WorksheetFunction.Find("=", temp)), vbInformation, "MATLAB Result"


                      End Sub



                      andd the Matlab code is like this :


                      function area = TrapezoidArea(LargeBase,SmallBase,Height)


                          % Calculates the area of a trapezoid given its dimensions.

                      % LargeBase and SmallBase are the lengths of the parallel sides

                      % and Height is the distance between these sides.

                      area = 0.5*(LargeBase+SmallBase)*Height;


                      so what I need is to excute this function in Matlab :    tic,[c,r] = minboundcircle(x,y);toc    by calling it using the vba code like presented in the last exemple and by defining firstly a set of point X and Y . are there some helps ?

                • Re: Bounding circle of 2D sketch
                  Keith Rice

                  I tried but gave up on a solution that tries to use the SolidWorks sketcher to effectively avoid complicated math, so here is a mathematical solution:


                  For a given set of points (P), there is a point C which is distance D from 3 of the points. The distance between all other points and C is <D. A circle created with center point at C and radius = D is the smallest possible circle that encompasses P.


                  I do not know how (or if it is possible) to calculate C directly, it could be discovered by iterating through all possible sets of 3 points and testing according to the above criteria. to minimize the iterations, one could find the 4 pts that share one of the values of P's bounding box. If 4 points are found, only 4 iterations are necessary. Obviously, if <4 points are in the sketch, no iterations are necessary.


                  I do not have time to program this. Do you see any problems with this approach?



                  SolidWorks API Training and Services