I am trying to rapidly select/find all shapes having the same angle inclination as a specific selected shape.
The most rapid and elegant method should be using of SQL query in a FindShapes function.
So, standard VBA 'Atn' function doing the same as SQL 'atan' works well but the one in SQL query returns an error. "Invalid argument for function 'atan'". Followed by the calculated value of the argument (correct calculated...).
Part of the code in discussion treats the 'problem' in a similar way but 'atan' returns that error. Like the value of the received argument would be not appropriate. But the 'atan' is described here like 'Returns the arctangent of the number'. And VBA standard 'Atn' function the same in some more words: "Returns a Double specifying the arctangent of a number"
This is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Sub testFindShapesSameAngle() Dim s As Shape, shR As ShapeRange, shRA As ShapeRange, ang As Double, x As Double, y As Double, xx As Double, yy As Double Dim tanVal As Double Set shR = ActiveSelectionRange Application.Unit = cdrMillimeter: ActiveDocument.Unit = cdrMillimeter Set s = shR.Shapes(1) x = s.Curve.Nodes.first.PositionX: y = s.Curve.Nodes.first.PositionY xx = s.Curve.Nodes.last.PositionX: yy = s.Curve.Nodes.last.PositionY ang = Round(Atn((yy - y) / (xx - x)) * 180 / (4 * Atn(1)), 2) Ang1 = Round(Atn((s.Curve.Nodes.last.PositionY - s.Curve.Nodes.first.PositionY) / _ (s.Curve.Nodes.last.PositionX - s.Curve.Nodes.first.PositionX)), 2) tanVal = (s.Curve.Nodes.last.PositionY - s.Curve.Nodes.first.PositionY) / _ (s.Curve.Nodes.last.PositionX - s.Curve.Nodes.first.PositionX) tanval1 = s.Evaluate("(@com.curve.nodes.last.positionY-@com.curve.nodes.first.positionY)/" & _ "(@com.curve.nodes.last.positionX-@com.curve.nodes.first.positionX)") Debug.Print tanVal, tanval1 'both return the same value Debug.Print Round(Atn(tanVal), 2) ' returns correct value (in radians). For the real angle it must be multiplied with 180/pi Debug.Print s.Evaluate("round(atan(" & tanval1 & "),2)") ' Here the error comes... End Sub
Am I doing something wrong? Or their function does not work as expected?
Thanks in advance!
It is obviously a Corel CQL bug...
But since 'tan' functions works well I can use it in order to do what I need. I can read the angle if I want using standard 'Tan' VBA function...
For somebody interested to see and understand what is the result look at the following code.
On active document, use a guide line, incline it and put on it some line shapes. As many as you want... Draw also a lot of other lines with different inclination angle.
You can also use any other curve shapes inclined (or not) at the same angle.
Select one shape on the inclined guide line and run the next code:
Sub testFindShapesSameAngle() Dim s As Shape, shR As ShapeRange, sh As Shape Dim x As Double, y As Double, xx As Double, yy As Double Set s = ActiveShape If s Is Nothing Then MsgBox "No shape selected...", vbInformation, "Selection missing": Exit Sub x = s.Curve.Nodes.first.PositionX: y = s.Curve.Nodes.first.PositionY xx = s.Curve.Nodes.last.PositionX: yy = s.Curve.Nodes.last.PositionY refTan = Round(Tan((yy - y) / (xx - x)), 2) Set shR = ActivePage.Shapes.FindShapes(, cdrCurveShape, True, _ Query:="round(tan((@com.curve.nodes.last.positionY-@com.curve.nodes.first.positionY)/" & _ "(@com.curve.nodes.last.positionX-@com.curve.nodes.first.positionX)),2)=" & refTan & "") Debug.Print shR.Shapes.Count: Stop ActiveDocument.ClearSelection For Each sh In shR.Shapes sh.CreateSelection: Stop Next End Sub
All curve shapes will be found and collected in ShR ShapeRange You will firstly see in Immediate Window the identically inclined shapes number and after that each shape will be selected one by one (at each F8 key pressed in IDE)...