Help! Why ShapeRange.Group returns "Nothing"?

Hi everybody!

I trying to script a macro that will draw lines in place of controlpoints arrows (of Bezier curves). My problem is (see row 37): Why ShapeRange.Group returns "Nothing"? But in other subroutine it working fine... Please help me - where is my error?

Generally I want to draw lines at selected nodes if it was selected... In other case - it must draw lines at starting and ending nodes of each subpaths of selected shapes. After all drawns - lines must be grouped and remain selected.

This is how I try to do it:

  1. Sub DrwCtrlPntLines()
  2. 'Draw lines in place of controlpoints arrows
  3. Dim OSel As ShapeRange
  4. Dim sr As ShapeRange
  5. Dim s As Shape
  6. Dim nr As New NodeRange
  7. Dim n As node
  8. Dim sp As SubPath
  9. Dim crv As Curve
  10. Set OSel = ActiveSelectionRange
  11. 'collect selected nodes
  12. For Each s In OSel
  13.   If (s.Type = cdrCurveShape) And (s.Curve.Selection.Count > 0) Then nr.AddRange s.Curve.Selection
  14. Next
  15. 'if nodes not selected - set begining and ending nodes
  16. If nr.Count = 0 Then
  17.   For Each s In OSel 'look in each shapes
  18.     If s.Type = cdrCurveShape Then 'of curve
  19.       For Each sp In s.Curve.SubPaths 'in it's each subpath
  20.         nr.Add sp.StartNode 'for one of the ending nodes
  21.         nr.Add sp.EndNode 'and collect it
  22.       Next sp
  23.     End If
  24.   Next s
  25. End If
  26. 'draw control point lines at collected nodes
  27. Set sr = New ShapeRange
  28. sr.RemoveAll
  29. For Each n In nr
  30.   Set s = ActiveLayer.CreateCurveSegment(n.PositionX, n.PositionY, _
  31.   n.Segment.EndingControlPointX, n.Segment.EndingControlPointY)
  32.   sr.Add s
  33. Next n
  34. Dim sg As Shape
  35. Set sg = sr.Group 'sg becomes Nothing when nodes was selected
  36. If Not sg Is Nothing Then sg.CreateSelection
  37. End Sub

Having looked through the help, I found that NodeRange can collect nodes only from a single curve. Also, I discovered that if we'll trying to group something, when ShapeTool (cdrToolNodeEdit) is active - ShapeRanges will be grouped, but as result always will return Nothing. So, before to group ShapeRanges, always you must switch to "Pick Tool" (cdrToolPick). And, if we made some selection with ShapeTool - we need also to clear all selections, before switching to Pick Tool.  So, I rewrote my code. And it work fine! Look my post below.

Parents
    1. Sub DrawControlPointLines()
    2. 'by Vanea
    3. 'Draw lines in place of controlpoints arrows at selected nodes,
    4. ' or at starting and ending nodes of selected curve shapes.
    5.   Dim OSel As ShapeRange
    6.   Dim sr As New ShapeRange
    7.   Dim s As Shape
    8.   Dim NSel As Long      'Nodes Selected
    9.   Dim n As node
    10.   Dim sp As SubPath
    11.  
    12.   Set OSel = ActiveSelectionRange
    13.  'draw lines at selected nodes
    14.   NSel = 0
    15.   For Each s In OSel
    16.     If s.Type = cdrCurveShape Then NSel = NSel + s.Curve.Selection.Count
    17.    'draw control point lines at selected nodes
    18.     If s.Curve.Selection.Count > 0 Then
    19.       For Each n In s.Curve.Selection
    20.        'If node is ending one
    21.         If n.IsEnding Then
    22.          ' and if it is at first segment -> draw only one line
    23.           If n.PrevSegment Is Nothing Then _
    24.             sr.Add ActiveLayer.CreateLineSegment(n.PositionX, _
    25.                                                  n.PositionY, _
    26.                                                  n.NextSegment.StartingControlPointX, _
    27.                                                  n.NextSegment.StartingControlPointY)
    28.          ' or if it is at last segment -> draw only one line
    29.           If n.NextSegment Is Nothing Then _
    30.             sr.Add ActiveLayer.CreateLineSegment(n.PositionX, _
    31.                                                  n.PositionY, _
    32.                                                  n.Segment.EndingControlPointX, _
    33.                                                  n.Segment.EndingControlPointY)
    34.         Else
    35.          'in case node is inside of subpath or on closed subpath -> draw lines in both sides
    36.             sr.Add ActiveLayer.CreateLineSegment(n.PositionX, _
    37.                                                  n.PositionY, _
    38.                                                  n.Segment.EndingControlPointX, _
    39.                                                  n.Segment.EndingControlPointY)
    40.             sr.Add ActiveLayer.CreateLineSegment(n.PositionX, _
    41.                                                  n.PositionY, _
    42.                                                  n.Segment.Next.StartingControlPointX, _
    43.                                                  n.Segment.Next.StartingControlPointY)
    44.         End If
    45.       Next n
    46.     End If
    47.   Next s
    48.  'if nodes not selected - draw lines at begining and ending nodes of each subpaths of selected curves
    49.   If NSel = 0 Then
    50.     For Each s In OSel                      'look in each shapes
    51.       If s.Type = cdrCurveShape Then        'of curve
    52.         For Each sp In s.Curve.SubPaths     'in it's each subpath
    53.         'draw control point lines at first segment and starting node
    54.           sr.Add ActiveLayer.CreateLineSegment(sp.FirstSegment.StartNode.PositionX, _
    55.                                                sp.FirstSegment.StartNode.PositionY, _
    56.                                                sp.FirstSegment.StartingControlPointX, _
    57.                                                sp.FirstSegment.StartingControlPointY)
    58.         'draw control point lines at last segment and ending node
    59.           sr.Add ActiveLayer.CreateLineSegment(sp.LastSegment.EndNode.PositionX, _
    60.                                                sp.LastSegment.EndNode.PositionY, _
    61.                                                sp.LastSegment.EndingControlPointX, _
    62.                                                sp.LastSegment.EndingControlPointY)
    63.         Next sp
    64.       End If
    65.     Next s
    66.     sr.Group.CreateSelection    'group and select lines drawn from starting and ending nodes of each subpaths of each selected shapes
    67.   Else
    68.    'group and select lines drawn from selected nodes
    69.     ActiveDocument.ClearSelection
    70.     ActiveTool = cdrToolPick
    71.     sr.Group.CreateSelection
    72.   End If
    73. End Sub 

Reply Children
No Data