plz tell me how to get mouse click point in VBA
sharav
Can you please tell me What value you assigned to this bObjSelected variable?
As while passing that variable in IndicateOrSelectElement2D method it is asking to assign some value.
Priyadharsini,
I think I may understand your question
bObjSelected is type boolean object. It is true if the user selected a point in the drawing. It returns false if the user clicks on a space of the empty sheet.
But I think what you are asking is why this section is required:
If bObjSelected Then
'An existing point was selected, get its coordinates
Set oPoint = oSel.Item(1).Value
oPoint.GetCoordinates dXY
End If
The CAA manual states that dXY would be:
An array made of 2 doubles: X, Y - coordinates array of the location the user specified in the document window. This parameter is valuated only if oObjectSelected equals to false.
But at least in CATIA R2018 the value of dXY will actually be input even if a point is selected. The problem is that the output of dXY will be the last valuation of "IndicateOrSelectElement2D" during that session. If oObjectSelected is true, then dXY is always incorrect. You can check the debug window with this modified version:
Sub CATMain()
Dim dXY(1)
Dim sSelType(0)
Set oDoc = CATIA.ActiveDocument
Set oSel = oDoc.Selection
oSel.Clear
sSelType(0) = "Point2D"
sSelStatus = oSel.IndicateOrSelectElement2D("Select a point or click", _
sSelType, False, False, False, bObjSelected, dXY)
If sSelStatus = "Normal" Then
Debug.Print dXY(0) & "," & dXY(1)
If bObjSelected Then
'An existing point was selected, get its coordinates
Set oPoint = oSel.Item(1).Value
oPoint.GetCoordinates dXY
End If
End If
oSel.Clear
Debug.Print dXY(0) & "," & dXY(1)
End Sub
Does that answer your question?
Hi Josh,
Thank you for your help.
I'll try this.
Hi Josh,
As mentioned, I have used that code for getting the x and y coordinates of the point where user is clicking.
I need those coordinates for generating "Detailed View" but I am getting some exceptions there.
So do you have any idea about it?
Priyadharsin,
Can you tell me about the exceptions?
One thing that comes to mind is that another view is active? The coordinates that are returned are based off the origin of the active view. If you modify the script like this, it may remove that potential:
Sub CATMain()
Dim dXY(1)
Dim sSelType(0)
Set oDoc = CATIA.ActiveDocument
Set oSel = oDoc.Selection
oDoc.Sheets.ActiveSheet.Views.Item("Main View").Activate 'ensure the active view is main view
oSel.Clear
sSelType(0) = "Point2D"
sSelStatus = oSel.IndicateOrSelectElement2D("Select a point or click", _
sSelType, False, False, False, bObjSelected, dXY)
If sSelStatus = "Normal" Then
'Debug.Print dXY(0) & "," & dXY(1)
If bObjSelected Then
'An existing point was selected, get its coordinates
Set oPoint = oSel.Item(1).Value
oPoint.GetCoordinates dXY
End If
End If
oSel.Clear
Debug.Print dXY(0) & "," & dXY(1)
End Sub
Hi, I have a similar need except mine is 3D coordinates with a mouse click. I would also like to snap to an object similar to the "hovering" or "picking point" feature in the measurement tool and convert the coordinates to A/C Coordinates upon "mouse click". I modified your code (see below) but I am getting runtime errors "Type mismatch: 'oSel.IndicateOrSelectElement3D' ". Can you tell me what I am missing? I have tried finding code to extract xyz parameters from the compass but have not had any luck there either.
Sub CATMain()
dim dXYZ(2)
dim sSelType(0)
set oDoc = CATIA.ActiveDocument
set oSel = oDoc.Selection
oSel.Clear
sSelType(0) = "Point3D"
sSelStatus = oSel.IndicateOrSelectElement3D(HybridShapePoint,
"Select a point or click", _
sSelType, false, false, false, bObjSelected, dXYZ)
if sSelStatus = "Normal" then
if bObjSelected then
'An existing point was selected, get its coordinates
set oPoint = oSel.Item(1).Value
oPoint.GetCoordinates dXYZ
end if
end if
oSel.Clear
msgbox dXYZ(0) & "," & dXYZ(1)
End Sub
Thanks,
Caleb
Caleb,
There are more requirements with the IndicateOrSelectElement3D. You also can't change that first object to a point - it has to be a plane. This one is tricky since you need to actually define a plane that the point or indication is supposed to lie. I'm not certain if this command is actually what you're looking for.
Are you just trying to find the coordinates of an already existing point or vertex?
Thanks for helping Josh. Originally (see incomplete code below), I was trying to extract the current position of the compass as we spend a lot of time manually typing xyz's from the compass, so I was hoping to automate that by extracting the xyz and posting it to a clipboard. The end user would then past the xyz from the clipboard to wherever they needed (e.g. excel, solumina, etc..). But the code didn't work and I haven't been able to find anything that can read the compass parameters. So plan B was to use the cursor similar to how the "picking point" function of the measurement tool works and extract an xyz to a clipboard...which lead me to this thread.
Sub CATMain()
Dim partDocument1 As Document
Set partDocument1 = CATIA.ActiveDocument
Dim part1 As Part
Set part1 = partDocument1.Part
Dim hybridShapeFactory1 As Factory
Set hybridShapeFactory1 = part1.HybridShapeFactory
Dim settingControllers1 As SettingControllers
Set settingControllers1 = CATIA.SettingControllers
Dim htsCCPSettingAtt1 As HtsCCPSettingAtt
Set htsCCPSettingAtt1 = settingControllers1. Item ("DNBHtsCCPSettingCtrl")
Dim x
Dim y
Dim z
x = htsCCPSettingAtt1.GetCompassPosInfo(0)
y = htsCCPSettingAtt1.GetCompassPosInfo(2)
z = htsCCPSettingAtt1.GetCompassPosInfo(4)
End Sub
Additionally, I looked at using send codes for the compass, but I don't think that will allow me to read the parameters either.
Caleb,
I think you could do this with an indicate or select element, but you'd need it to be pretty involved and it would be really difficult with highly contour surfaces.
If you're doing this many times on a specific part, why not just create a geometric set where you create points all over the surface and then have a macro that returns all those values?
Caleb,
Not all functions of CATIA have been tied to VBA objects. There is a lot that you cannot do. The compass is one of them. Also, the SettingControllers objects only change things under tools - options. There is no access to the compass object through that.
The example the CAA documentation gives for the IndicateOrSelectElement3D command include creating a point and a plane. If the QC team is stuck with an DM1 license or something similar, and cannot make geometry, you won't be able to utilize the command. At the bare minimum, it appears that CATIA needs to build a limit to what the user is selecting which is why it needs to be on a plane or a planar feature.
Hi Caleb!
Here is one way you can do it in 3D using
"IndicateOrSelectElement3D".
How it works:
-Creates a plane parallel to screen
-Looping the "IndicateOrSelectElement3D" until left-button
click
-While looping it checks if the view has changed or not, if changed
it creates a new parallel plane.
-If not an existing point or vertex is chosen it creates a
point where clicked and projects it on the surface (Extract.1)
-If clicked outside of surface it sends a message and
ends.
Needed for it to run: Part->"Geometrical
Set.1"->"Extract.1".
Enjoy!
Dim odocument, opart, hsf, hbs, hb
Sub CATMain()
On Error Resume Next
Set odocument = CATIA.ActiveDocument
Set opart = odocument.Part
Set hsf = opart.HybridShapeFactory
Set hbs = opart.HybridBodies
Set hb = hbs.Item("Geometrical Set.1")
If Err.Number <> 0 Then
'error handling
MsgBox "No part found"
End If
On Error GoTo 0
Dim scr_pln_res
Set scr_pln_res = scr_pln()
'create reference to sigh direction plane
Dim scr_pln_ref
Set scr_pln_ref = opart.CreateReferenceFromObject(scr_pln_res)
'indicate or select 3D
Dim oselection
Set oselection = odocument.Selection
Dim input_type(1), location_2D(1), location_3D(2), status, obj_selected, existing_point
'set whats allowed to select
input_type(0) = "Point"
input_type(1) = "Vertex"
status = "MouseMove"
oselection.Clear
status = oselection.IndicateOrSelectElement3D(scr_pln_res, "Select a point or click to locate the point", input_type, False, False, True, obj_selected, location_2D, location_3D)
'get in value for sight direction coords
Dim in_coords
in_coords = sight_coords()
Dim in_coordx
in_coordx = in_coords(0)
Dim out_coords, out_coordx
'do loop while checking if viewer direction is modified
Do While (status = "MouseMove")
out_coords = sight_coords()
out_coordx = out_coords(0)
If in_coordx = out_coordx Then
status = oselection.IndicateOrSelectElement3D(scr_pln_res, "Select a point or click to locate the point", input_type, False, False, True, obj_selected, location_2D, location_3D)
Else
hsf.DeleteObjectForDatum scr_pln_ref
Set scr_pln_res = scr_pln()
Set scr_pln_ref = opart.CreateReferenceFromObject(scr_pln_res)
status = oselection.IndicateOrSelectElement3D(scr_pln_res, "Select a point or click to locate the point", input_type, False, False, True, obj_selected, location_2D, location_3D)
in_coords = sight_coords()
in_coordx = in_coords(0)
End If
Loop
'if bailing
If (status = "Cancel" Or status = "Undo" Or status = "Redo") Then
hsf.DeleteObjectForDatum scr_pln_ref
Exit Sub
End If
'if selecting existing point or vertex
If (obj_selected) Then
Dim vertex_point, coords(2)
Set existing_point = oselection.Item2(1)
existing_point.GetCoordinates coords
Set vertex_point = hsf.AddNewPointCoord(coords(0), coords(1), coords(2))
hb.AppendHybridShape vertex_point
oselection.Clear
opart.UpdateObject vertex_point
Dim vertex_point_ref
Set vertex_point_ref = opart.CreateReferenceFromObject(vertex_point)
Dim vertex_point_datum
Set vertex_point_datum = hsf.AddNewPointDatum(vertex_point_ref)
hb.AppendHybridShape vertex_point_datum
opart.UpdateObject vertex_point_datum
hsf.DeleteObjectForDatum vertex_point_ref
hsf.DeleteObjectForDatum scr_pln_ref
Exit Sub
End If
'or if clicked to create a new point intersecting with surface
'create screen point
Dim scr_point
Set scr_point = hsf.AddNewPointCoord(location_3D(0), location_3D(1), location_3D(2))
hb.AppendHybridShape scr_point
'update
opart.UpdateObject scr_point
'create reference to screen point
Dim scr_point_ref
Set scr_point_ref = opart.CreateReferenceFromObject(scr_point)
'project on surface
Dim hs
Set hs = hb.HybridShapes
On Error Resume Next
'get surface
Dim project_surface
Set project_surface = hs.Item("Extract.1")
'create reference to surface
Dim project_surface_ref
Set project_surface_ref = opart.CreateReferenceFromObject(project_surface)
Dim projected_point
Set projected_point = hsf.AddNewProject(scr_point_ref, project_surface_ref)
'get sight direction coords
Dim sight_dir_coords
sight_dir_coords = sight_coords()
'create sight direction
Dim sight_dir
Set sight_dir = hsf.AddNewDirectionByCoord(sight_dir_coords(0), sight_dir_coords(1), sight_dir_coords(2))
projected_point.Normal = False
projected_point.Direction = sight_dir
hb.AppendHybridShape projected_point
opart.UpdateObject projected_point
Dim projected_point_ref
Set projected_point_ref = opart.CreateReferenceFromObject(projected_point)
Dim projected_point_datum
Set projected_point_datum = hsf.AddNewPointDatum(projected_point_ref)
hb.AppendHybridShape projected_point_datum
opart.UpdateObject projected_point_datum
' Delete history
hsf.DeleteObjectForDatum projected_point_ref
hsf.DeleteObjectForDatum scr_point_ref
hsf.DeleteObjectForDatum scr_pln_ref
If Err.Number <> 0 Then
'error handling
MsgBox "Not possible to project point"
End If
End Sub
Function sight_coords()
'get viewer object
Dim oviewer
Set oviewer = CATIA.ActiveWindow.ActiveViewer
'get viewpoint object
Dim oviewpoint
Set oviewpoint = oviewer.Viewpoint3D
Dim sight(2)
oviewpoint.GetSightDirection sight
sight_coords = sight
End Function
Function scr_pln()
'get viewer object
Dim oviewer
Set oviewer = CATIA.ActiveWindow.ActiveViewer
'get viewpoint object
Dim oviewpoint
Set oviewpoint = oviewer.Viewpoint3D
'get viewpoint coords
Dim viewpoint_coords(2)
oviewpoint.GetOrigin viewpoint_coords
'create viewpoint
Dim origin_point
Set origin_point = hsf.AddNewPointCoord(viewpoint_coords(0), viewpoint_coords(1), viewpoint_coords(2))
'create reference to viewpoint
Dim origin_point_ref
Set origin_point_ref = opart.CreateReferenceFromObject(origin_point)
'get sight direction coords
Dim sight_dir_coords
sight_dir_coords = sight_coords()
'create sight direction
Dim sight_dir
Set sight_dir = hsf.AddNewDirectionByCoord(sight_dir_coords(0), sight_dir_coords(1), sight_dir_coords(2))
'create sight direction line
Dim sight_dir_line
Set sight_dir_line = hsf.AddNewLinePtDir(origin_point_ref, sight_dir, 0#, 20#, False)
'create reference to sight direction line
Dim sight_dir_line_ref
Set sight_dir_line_ref = opart.CreateReferenceFromObject(sight_dir_line)
'create sight direction plane
Set scr_pln = hsf.AddNewPlaneNormal(sight_dir_line_ref, origin_point_ref)
hb.AppendHybridShape scr_pln
'update
opart.UpdateObject scr_pln
End Function