Arbitrary Circle Sketched in space

Discussion in 'SolidWorks' started by Duva, Nov 3, 2007.

  1. Duva

    Duva Guest

    SW fans
    I am having an issue defining (from VB code) a sketch in space with
    coordinates x1,y1,z1 and with radius r1.
    I have currently sussed out the code to define an arbitrary plane
    where I want, but I am only able to sketch circles properly in the XY
    plane.

    This is primarily with the code:
    (after generating and selecting a plane where I want the circle).

    Part.CreateCircleByRadius2 0.48, -0.135, 0.3727, 0.1

    All circular sketches in the XZ,YZ (and arbitrary) plane take a
    different input into the function which just seems stupid.

    1) Is there a better function to use? If so, what is it?
    2) If not how can I go about defining these circles.

    All commentary is appreciated.
     
    Duva, Nov 3, 2007
    #1
  2. Duva

    Heikki Leivo Guest

    I am having an issue defining (from VB code) a sketch in space with
    Am I right if I suggest that you are having problems with the centerpoint
    coordinates and you have to use different coordinates for the very same
    location depending on the sketch plane orientation?

    If this is the case, you should use matrices to transform beetween model and
    sketch space coordinates. You can use Sketch::ModelToSketchTransform
    property to get a MathTransform object which represents the required
    transform matrix. You can use MathPoint objects to represent 3D points in
    space, and you can use the MathPoint::MultiplyTransform method to go from
    model coordinates to sketch coordinates. You can use MathTransform::Inverse
    to get a matrix which represents the transform from sketch coordinates to
    model coordinates.

    Hope this helps!

    -h-
     
    Heikki Leivo, Nov 3, 2007
    #2
  3. Duva

    Duva Guest

    The centrepoint seems to jump around (i.e. its not the global CS, its
    relative to the plane that I'm on.)
    The matrix idea seems a little to complicated, as I only need to do
    this a few times.
    Your initial suggestion seems on target, just that I am looking for
    something simple
    Any other ideas?
     
    Duva, Nov 3, 2007
    #3
  4. Duva

    Heikki Leivo Guest

    The centrepoint seems to jump around (i.e. its not the global CS, its
    It is not complicated, and the matrices are the ultimate solution to your
    problem. Please see the following example. You just have to create the point
    object and multiply it with the model->sketch transform, and that's it. Note
    that you don't have to actually understand the mathematics used in the
    background.

    Option Explicit
    Sub main()
    'Matrix demo
    'We assume that a model is open and a sketch is active
    Dim swApp As SldWorks.SldWorks
    Set swApp = Application.SldWorks

    Dim model As SldWorks.ModelDoc2
    Set model = swApp.ActiveDoc
    Debug.Assert Not (model Is Nothing)

    'Get the active sketch
    Dim activeSketch As SldWorks.Sketch
    Set activeSketch = model.SketchManager.activeSketch

    'If there is no active sketch, create one
    If activeSketch Is Nothing Then
    model.SketchManager.InsertSketch False
    Set activeSketch = model.SketchManager.activeSketch
    End If

    Debug.Assert Not (activeSketch Is Nothing)

    'Get the math utility to access vector & matrix stuff
    Dim mathUtility As SldWorks.mathUtility
    Set mathUtility = swApp.GetMathUtility


    'Define the centerpoint coordinates in global coordinate system (put
    your own values here)
    Dim globalCoordinates(0 To 2) As Double
    globalCoordinates(0) = 0.05
    globalCoordinates(1) = 0.05
    globalCoordinates(2) = 0.05

    'Define radius
    Dim radius As Double
    radius = 0.05

    'Create a MathPoint object representing the centerpoint
    Dim centerPoint As SldWorks.MathPoint
    Set centerPoint = mathUtility.CreatePoint(globalCoordinates)

    'Get the transform matrix from global coodinate system to sketch
    coordinates
    Dim modelToSketchTransform As SldWorks.MathTransform
    Set modelToSketchTransform = activeSketch.modelToSketchTransform

    'Transform the centerpoint coordinates to sketch coordinates
    Set centerPoint = centerPoint.MultiplyTransform(modelToSketchTransform)

    'Get the transformed coordinate values from the point object
    Dim transformedCoordinates As Variant
    transformedCoordinates = centerPoint.ArrayData

    'Finally create the circle
    model.SketchManager.CreateCircleByRadius transformedCoordinates(0),
    transformedCoordinates(1), transformedCoordinates(2), radius
    End Sub
     
    Heikki Leivo, Nov 3, 2007
    #4
  5. Duva

    Duva Guest

    Heikki, that certainly seems promising, yet for me that code has
    runtime error 438, Object does not support this property or method
    on: line: model.SketchManager.InsertSketch False
    Running SW 2006
    I have an open SW model, and tried with both a 3d and normal sketch
    open.
     
    Duva, Nov 4, 2007
    #5
  6. Duva

    Heikki Leivo Guest

    Heikki, that certainly seems promising, yet for me that code has
    It seems that the SketchManager::InsertSketch method is not available in
    SW2006. You can use ModelDoc2::InsertSketch and ModelDoc2::GetActiveSketch
    instead. In SW2007 the sketch methods have been grouped to SketchManager
    object. Change the code to

    'Get the active sketch
    Dim activeSketch As SldWorks.Sketch
    ' Set activeSketch = model.activeSketch

    'If there is no active sketch, create one
    If activeSketch Is Nothing Then
    model.InsertSketch
    Set activeSketch = model.activeSketch
    End If
     
    Heikki Leivo, Nov 4, 2007
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.