5 Replies Latest reply on Jul 7, 2017 6:05 AM by Peter Brinkhuis

    How to Create a B-Spline Surface

    Tim Fromme



      I have all the data necessary to create B-Spline Surfaces, however the API is not very clear to me with them.  There is CreateBSplineSurface, ICreateBSplineSurface, and ICreateBSplineSurfaceDLL which all seem to do the same thing.  I have tried a lot of different combinations of formatting the input to these functions, but I always seem to get an object variable not set error or a type mismatch error.  The first two functions ask for objects as input, and the last function asks for a long and three doubles, but the documentation and required input suggests that they should be arrays of some kind.  I have coding experience, but have not used VBA in forever.



      I have since figured out why I was getting errors, it had to do with not instantiating the body as a new body in a part.  I am getting no more errors, however I crash solidworks every time that I try to run the macro, on the function call for CreateBSplineSurface or ICreateBSplineSurface.




      I have been able to get this method working in C#, with separate issues that I feel are different enough to merit opening a different discussion on.

      How to trim a closed surface?


      Thanks in advance.

        • Re: How to Create a B-Spline Surface
          Peter Brinkhuis

          Can you share your code?


          You could prevent crashes by adding lines like the following:


          On Error GoTo LabelX

            code that can cause an error


            Label you jump to when an error occurs.


          We might be able to prevent the error from happening altogether though

            • Re: How to Create a B-Spline Surface
              Tim Fromme

              Here is the relevant code:

              Sub main()

              Dim swApp As Object

              Set swApp = Application.SldWorks

              Dim swModel As SldWorks.IModelDoc2

              Set swModel = swApp.ActiveDoc

              Dim swPart As SldWorks.IPartDoc

              Set swPart = swModel


              Dim body As IBody2

              Set body = swPart.ICreateNewBody2


              Dim Props(7) As Integer

              Dim UKnots(5) As Double

              Dim VKnots(5) As Double

              Dim CtrlPtCoords(26) As Double


              Props(0) = 3


              Props(7) = 0


              UKnots(0) = 0#


              UKnots(5) = 1#


              VKnots(0) = 0#


              VKnots(5) = 1#


              CtrlPtCoords(0) = 0#


              CtrlPtCoords(26) = 0#


              Dim surface As ISurface

              Set surface = body.ICreateBsplineSurface(Props, UKnots, VKnots, CtrlPtCoords) LINE CAUSING ERROR



              End Sub


              The error is happening on pretty much the most important line in the program, and always gives an Access Violation Error.

              Crash occurs on SW16 and 17 when there is a part open, without a part open there is a different error relating to the activeDoc line, which makes sense.

                • Re: How to Create a B-Spline Surface
                  Peter Brinkhuis

                  You could start by changing IPart to Part etc for all objects, otherwise you are apparently defining interfaces instead of objects.


                  You might also need some checks to make sure that "body" is not nothing. If it is, you shouldn't perform any actions on it. The same holds for parts, you can check the type before setting swPart equal to swModel.


                  Regarding the parameters, they can be messy sometimes. Certainly when the help says something like "These are integers packed into a double array", while it is supposed to be an object. An example would really help there, because I also don't know exactly what to feed this function. I might try creating an array and copying it to an object before passing it as a parameter.

                    • Re: How to Create a B-Spline Surface
                      Tim Fromme

                      After changing the interfaces to objects and doing some checks, it looks like the issue is in how I am passing the data to the function.  Will look at more next week. :/ 

                      Side note: I only really need to get raw nurb data into solidworks, I don't care how I do it.  Saving to IGES and 3DM didn't work as well as I would have hoped, but if anyone has suggestions in general for this, that would be greatly appreciated.

                        • Re: How to Create a B-Spline Surface
                          Peter Brinkhuis

                          I tried the same. It just crashes on the troubled line, even my error handling doesn't catch it. It would have been nice if they would make a function a little more robust when it is so hard to create a set of proper parameters.


                          I have no idea how to create a set of parameters that should work. There are a few forum questions about this function, but no real answers. I haven't worked with either splines or surfaces, so that makes it harder I also couldn't find a button to create such a surface in the GUI, so it's unclear to me what it should do exactly. If there is a button available, maybe we can record an example.