The following code example shows how you can create
and position a bubble ToolTip in SolidWorks screen space using the
SolidWorks API and Microsoft Windows API.
'-------------------------------------------------------------------------------
'
' Preconditions:
' 1. SolidWorks document is active.
' 2. Bubble Tooltip image file or HTML files are
' in c:\temp.
'
' Postconditions:
' 1. Image file or HTML files are displayed
' in a bubble ToolTip at the specified
' location.
' 2. Bubble ToolTip is hidden.
'
'-------------------------------------------------------------------------------
Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
' Microsoft Windows API
Declare Function ClientToScreen
Lib "user32" (ByVal
hwnd As Long,
_
lpPoint As POINTAPI)
As Long
Dim swApp As
SldWorks.SldWorks
Dim swModel As
SldWorks.ModelDoc2
Dim swSketch As
SldWorks.Sketch
Dim swFeat As
SldWorks.Feature
Dim SelMgr As
SldWorks.SelectionMgr
Dim boolstatus As
Boolean
Dim nPts(2) As
Double
Dim swMathUtil As
SldWorks.MathUtility
Dim swMathPt As
SldWorks.MathPoint
Dim view As
SldWorks.ModelView
Dim MVXform As
SldWorks.MathTransform
Dim swModelViewMathPt
As SldWorks.MathPoint
Dim ModelXform As
SldWorks.MathTransform
Dim vSketchSegs
As Variant
Dim SketchLine As
SldWorks.SketchLine
Dim SketchPt As
SldWorks.SketchPoint
Dim lpPoint As
POINTAPI
Sub main()
' Path and file name of bubble
' ToolTip image file
Const sURLpath As
String = "c:/temp/Error.jpg"
' Comment out previous statement and
' uncomment following statement to
' show HTML files instead of image
' in bubble ToolTip
'Const sURLpath As String = "c:/temp/sampleBubbleToolTip.html"
Set swApp = Application.SldWorks
' Get the active document
Set swModel = swApp.ActiveDoc
swModel.ViewZoomtofit2
Set SelMgr = swModel.SelectionManager
Set swMathUtil = swApp.GetMathUtility
' Get
the active view
Set view = swModel.ActiveView
' Select a sketch and get its sketch segments
boolstatus = swModel.Extension.SelectByID2 _
("Sketch1", "SKETCH", 0, 0, 0, False,
0, Nothing, 0)
Set swFeat =
SelMgr.GetSelectedObject6(1, -1)
Set swSketch =
swFeat.GetSpecificFeature2
vSketchSegs = swSketch.GetSketchSegments
' Get either the end point of
' a sketch segment or its start point
Set SketchLine = vSketchSegs(0)
Set SketchPt = SketchLine.GetEndPoint2
' Comment out previous statement and uncomment
' following statement to use start point of
' sketch segment instead of its end point
'Set SketchPt = SketchLine.GetStartPoint2
' Get the x, y, z coordinates of the sketch
point
nPts(0) = SketchPt.X
nPts(1) = SketchPt.Y
nPts(2) = SketchPt.Z
Set swMathPt =
swMathUtil.CreatePoint(nPts)
Debug.Print swFeat.Name & " location is
" & "(" _
& nPts(0) & "," & nPts(1) & "," & nPts(2) & ")"
' Transform from sketch to model
Set ModelXform =
swSketch.ModelToSketchTransform
Set ModelXform = ModelXform.Inverse
Set swMathPt =
swMathPt.MultiplyTransform(ModelXform)
' Transform from model to model view
Set MVXform = view.Transform
Set swModelViewMathPt =
swMathPt.MultiplyTransform(MVXform)
Debug.Print "ModelView location in pixels is " & "(" _
& Round(swModelViewMathPt.ArrayData(0), 0) & "," & _
Round(swModelViewMathPt.ArrayData(1),
0) & ")"
' Call Microsoft API ClientToScreen function
to convert
' SolidWorks model view coordinates of specified
' point to screen coordinates
lpPoint.X = swModelViewMathPt.ArrayData(0)
lpPoint.Y = swModelViewMathPt.ArrayData(1)
Call ClientToScreen(view.GetViewHWnd,
lpPoint)
Debug.Print "Screen coordinates\pixels
are " & "(" & _
lpPoint.X & "," & lpPoint.Y & ")" & vbCrLf
' Display the image in the bubble ToolTip at
specified point
swApp.ShowBubbleTooltipAt lpPoint.X, lpPoint.Y, swArrowRightTop, _
"", "", sURLpath
' Comment out previous statement and uncomment
' following statement to display HTML files instead
' of image
'swApp.ShowBubbleTooltipAt lpPoint.X, lpPoint.Y, swArrowLeftTop, _
"Example", "Message", sURLpath
' Verify bubble ToolTip was displayed
' by examining the SolidWorks graphics area,
' then click the Continue button in the IDE
Stop
' Hide bubble ToolTip
swApp.HideBubbleTooltip
' Deselect the sketch
swModel.ClearSelection2 True
End Sub
'--------------------------------------------