How to calculate CreatePoint in drawing file via midpoint ?

I can't create point at the MidPoint position.

How can I draw Point at the correct position ?

I got EdgePoint datum and got xyz by using EdgePoint.GetPointCoordinates method.

I run CreatePoint method, but its position is wrong.

    ' Select MidPoint
    swModel.SelectMidpoint

    ' Get selected EDGE
    Dim edgep As SldWorks.EdgePoint
    Set edgep = swSelMgr.GetSelectedObject6(2, -1)  ' first:Edge, second:EdgePoint(PointRef)
    Dim swEdgePt As EdgePoint
    Set swEdgePt = edgep
   
   ' Get MidPoint Coordinates
    Dim mx As Double
    Dim my As Double
    Dim mz As Double
    Call swEdgePt.GetPointCoordinates(mx, my, mz)
   
    Dim mPt(0 To 2) As Double
    mPt(0) = mx
    mPt(1) = my
    mPt(2) = mz
   
   ' Create Point at the midpoint position
    ' But its position is wrong.
    Dim skPt As SketchPoint
    Set skPt = swModel.SketchManager.CreatePoint(mPt(0), mPt(1), mPt(2))
  

Thanks.

SolidworksApi macros
Answers 6
MB
MB 2017-08-07 Best Answer

Hi Sayuri,

Please try this below code to create point in selected edge midpoint.

Dim swApp As SldWorks.SldWorks

Dim swmodel As SldWorks.ModelDoc2

Dim swSelMgr As SldWorks.SelectionMgr

Dim boolstatus As Boolean

Dim swedgept As SldWorks.EdgePoint

Dim swview As SldWorks.View

Dim swdraw As SldWorks.DrawingDoc

Dim swfeat As SldWorks.Feature

Dim swViewXform As SldWorks.MathTransform

Dim skPt As SketchPoint

Dim swMathUtil As SldWorks.MathUtility

Dim swModelMidPt As SldWorks.MathPoint

Dim swViewMidPt As SldWorks.MathPoint

Dim x As Double

Dim y As Double

Dim z As Double

Dim nPtData(2) As Double

Dim vPtData   As Variant

  

Sub main()

Set swApp = Application.SldWorks

Set swmodel = swApp.ActiveDoc

Set swdraw = swmodel

Set swSelMgr = swmodel.SelectionManager

Set swMathUtil = swApp.GetMathUtility

Set swfeat = swmodel.FeatureByName("Drawing view1")

Set swview = swfeat.GetSpecificFeature2

boolstatus = swmodel.Extension.SelectByRay(0.392427213904828, 0.466024887472847, 4.99999999999545E-03, 0, 0, -1, 2.78453424783087E-03, 1, False, 0, 0)

swmodel.SelectMidpoint

Set swedgept = swSelMgr.GetSelectedObject6(2, -1)

swedgept.GetPointCoordinates x, y, z

nPtData(0) = x

nPtData(1) = y

nPtData(2) = z

vPtData = nPtData

Set swModelMidPt = swMathUtil.CreatePoint(vPtData)

Set swViewXform = swview.ModelToViewTransform

Set swViewMidPt = swModelMidPt.MultiplyTransform(swViewXform)

x = swViewMidPt.ArrayData(0)

y = swViewMidPt.ArrayData(1)

z = swViewMidPt.ArrayData(2)

swmodel.SketchManager.CreatePoint x, y, z

End Sub

Manikandan

comment imagecomment image
NS
NS 2017-08-08

Hi Manikandan, thank you so much.

Your code worked fine.

But mycode fails at

swmodel.SketchManager.CreatePoint x, y, z

' ----- YOUR CODE --------------------------------------------

boolstatus = swmodel.Extension.SelectByRay(4.02766871648805E-02, 8.54934081756364E-02, 4.99999999999545E-03, 0, 0, -1, 1.41495828362914E-03, 1, False, 0, 0)

swmodel.SelectMidpoint

Set swedgept = swSelMgr.GetSelectedObject6(1, -1)

' ----- MY CODE   (run after edge is selected.) ----------

swmodel.SelectMidpoint

Set swedgept = swSelMgr.GetSelectedObject6(2, -1)

x, y, z values are same. (your code VS my code)

I got correct position, so I think that my CreatePoint code is wrong.

I'm confused...

comment imagecomment image
MB
MB 2017-08-08

Hi Sayuri,

Can you provide the full code and sample file

Thanks

Manikandan

NS
NS 2017-08-08

Hi Manikandan, thanks for your reply.

I sent my files and VBA code.

sub: midpt1  is your code. (It's OK)

sub: midpt2  is my code. (It's NG)

MB
MB 2017-08-08

Hi Sayuri,

Try this code:Selected the edge through API.

Sub midpt2()

    Set swApp = Application.SldWorks

    Set swmodel = swApp.ActiveDoc

    Set swdraw = swmodel

    Set swSelMgr = swmodel.SelectionManager

    Set swMathUtil = swApp.GetMathUtility

    'Set swfeat = swmodel.FeatureByName("}–ÊËÞ­°1")

    'Set swview = swfeat.GetSpecificFeature2

   

    Set swview = swdraw.GetFirstView

    Set swview = swview.GetNextView

   

   'Select the edge through API

    boolstatus = swmodel.Extension.SelectByRay(9.28394399014157E-02, 0.150636721358227, 4.99999999999545E-03, 0, 0, -1, 1.05028285131859E-03, 1, False, 0, 0)

    ' MidPoint   (Edge line's already seleccted)

    swmodel.SelectMidpoint

   

    Dim nCount As Integer

    nCount = swSelMgr.GetSelectedObjectCount

   

    ' Get MidPoint

    Set swedgept = swSelMgr.GetSelectedObject6(nCount, -1)

   

    swedgept.GetPointCoordinates x, y, z

    nPtData(0) = x

    nPtData(1) = y

    nPtData(2) = z

    vPtData = nPtData

   

    Set swModelMidPt = swMathUtil.CreatePoint(vPtData)

    Set swViewXform = swview.ModelToViewTransform

    Set swViewMidPt = swModelMidPt.MultiplyTransform(swViewXform)

   

    x = swViewMidPt.ArrayData(0)

    y = swViewMidPt.ArrayData(1)

    z = swViewMidPt.ArrayData(2)

   

    ' CreatePoint in drawing

    swmodel.SketchManager.CreatePoint x, y, z

End Sub

Manikandan

comment imagecomment image
NS
NS 2017-08-08

Hi, Manikandan

I found the condition that  [my code & your code] fails.

1) When sheet is selected  (therefore no view is selected), it works fine!

2) When one view is selected, it fails.

In case of 1)  we must consider view offset.

----------------------------------------------------------------------

   ( your code or my code)

    x = swViewMidPt.ArrayData(0)

    y = swViewMidPt.ArrayData(1)

    z = swViewMidPt.ArrayData(2)

   

    ' Get View Offset

    Dim vViewOffset As Variant

    vViewOffset = swview.Position

   

    Dim offsetX As Double

    Dim offsetY As Double

   

    offsetX = vViewOffset(0)

    offsetY = vViewOffset(1)

   

    x = x - offsetX      ' subtract view offset X

    y = y - offsetY      ' subtract view offset Y

    ' CreatePoint in drawing

    swmodel.SketchManager.CreatePoint x, y, z

----------------------------------------------------------------------

Thank you for your help.

I found a solution.

Thank you, Manikandan

comment imagecomment imagecomment imagecomment image