From: Duva on
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.

From: Heikki Leivo on
> 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.

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-


From: Duva on
On Nov 3, 9:05 pm, "Heikki Leivo"
<heikkidotle...(a)cadworksdotfi.removethis> wrote:
> > 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.
>
> 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-

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?

From: Heikki Leivo on
> 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

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




From: Duva on
On Nov 4, 1:11 am, "Heikki Leivo"
<heikkidotle...(a)cadworksdotfi.removethis> wrote:
> > 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
>
> 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, 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.



 |  Next  |  Last
Pages: 1 2
Prev: GB/T1804-m
Next: Warning: sketch text update failed