Rosette Tools Demo#

Demonstration of Rosette Tool functionality in Dips Python API.

This script demonstrates all Rosette tool services including: - Tool Group Visibility: All tools, Text, Arrow, Line, Polyline, Polygon, Rectangle, Ellipse, TrendLine - Tool CRUD operations: Add, Get, Update, Delete, SetVisibility for 8 tool types

Rosette supports 8 tool types (fewer than Stereonet2D which has 13): - Text, Arrow, Line, Polyline, Polygon, Rectangle, Ellipse, TrendLine

Note: General Rosette functionality is demonstrated in RosetteDemo.py

Code Snippet: RosetteToolsDemo.py#
"""
Demonstration of Rosette Tool functionality in Dips Python API.

This script demonstrates all Rosette tool services including:
- Tool Group Visibility: All tools, Text, Arrow, Line, Polyline, Polygon, Rectangle, Ellipse, TrendLine
- Tool CRUD operations: Add, Get, Update, Delete, SetVisibility for 8 tool types

Rosette supports 8 tool types (fewer than Stereonet2D which has 13):
- Text, Arrow, Line, Polyline, Polygon, Rectangle, Ellipse, TrendLine

Note: General Rosette functionality is demonstrated in RosetteDemo.py
"""

import math
from dips import DipsApp
from dips import BuiltInDataFormatters
from dips import DipsAPI_pb2

# Helper value wrappers
import dips.TrendPlungeVal
import dips.AngleDataVal
import dips.ColorSurrogateVal
import dips.AnchorPointVal
import dips.Vector2DVal
import dips.LineFormatVal
import dips.FillFormatVal
import dips.TextFormatVal


# =============================================================================
# HELPER FUNCTIONS
# =============================================================================

def create_trend_plunge(trend_deg: float, plunge_deg: float):
    """Create a TrendPlunge from degrees."""
    tp = dips.TrendPlungeVal.TrendPlungeVal()
    tp.trend = dips.AngleDataVal.AngleDataVal()
    tp.trend.angle_radians = math.radians(trend_deg)
    tp.plunge = dips.AngleDataVal.AngleDataVal()
    tp.plunge.angle_radians = math.radians(plunge_deg)
    return tp


def create_color(r: int, g: int, b: int, a: int = 255):
    """Create a Color from RGBA values."""
    color = dips.ColorSurrogateVal.ColorSurrogateVal()
    color.r = r
    color.g = g
    color.b = b
    color.a = a
    return color


def create_anchor_point(trend_deg: float, plunge_deg: float):
    """Create an AnchorPoint from degrees using spherical coordinates."""
    ap = dips.AnchorPointVal.AnchorPointVal()
    ap.coordinate_option = DipsAPI_pb2.eAnchorCoordinateOption.Spherical
    ap.spherical_point = create_trend_plunge(trend_deg, plunge_deg)
    return ap


def create_vector2d(x: float, y: float):
    """Create a Vector2D."""
    v = dips.Vector2DVal.Vector2DVal()
    v.x = x
    v.y = y
    return v


def create_line_format(color_rgb: tuple, width: float = 1.0):
    """Create a LineFormat with specified color and width."""
    lf = dips.LineFormatVal.LineFormatVal()
    lf.color = create_color(*color_rgb)
    lf.line_width = width
    return lf


def create_text_tool(model, name: str, text: str, trend: float, plunge: float):
    """Create a configured TextToolEntityInfo."""
    tool = model.GetDefaultTextToolEntityInfo()
    tool.name = name
    tool.text = text
    tool.is_visible = True
    tool.anchor_point = create_anchor_point(trend, plunge)
    return tool


def create_arrow_tool(model, name: str, start_trend: float, start_plunge: float, end_trend: float, end_plunge: float):
    """Create a configured ArrowToolEntityInfo."""
    tool = model.GetDefaultArrowToolEntityInfo()
    tool.name = name
    tool.is_visible = True
    tool.anchor_point = create_anchor_point(start_trend, start_plunge)
    tool.anchor_point_secondary = create_anchor_point(end_trend, end_plunge)
    return tool


def create_line_tool(model, name: str, start_trend: float, start_plunge: float, end_trend: float, end_plunge: float):
    """Create a configured LineToolEntityInfo."""
    tool = model.GetDefaultLineToolEntityInfo()
    tool.name = name
    tool.is_visible = True
    tool.anchor_point = create_anchor_point(start_trend, start_plunge)
    tool.anchor_point_secondary = create_anchor_point(end_trend, end_plunge)
    return tool


# =============================================================================
# TOOL GROUP VISIBILITY DEMONSTRATIONS
# =============================================================================

def demonstrate_tool_group_visibility(view):
    """Demonstrate all tool group visibility settings."""
    print("\n" + "=" * 60)
    print("TOOL GROUP VISIBILITY")
    print("=" * 60)
    
    try:
        view.SetToolsEntityGroupVisibility(True)
        print("  ✓ SetToolsEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetToolsEntityGroupVisibility: {e}")
    
    try:
        view.SetTextToolEntityGroupVisibility(True)
        print("  ✓ SetTextToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetTextToolEntityGroupVisibility: {e}")
    
    try:
        view.SetArrowToolEntityGroupVisibility(True)
        print("  ✓ SetArrowToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetArrowToolEntityGroupVisibility: {e}")
    
    try:
        view.SetLineToolEntityGroupVisibility(True)
        print("  ✓ SetLineToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetLineToolEntityGroupVisibility: {e}")
    
    try:
        view.SetPolylineToolEntityGroupVisibility(True)
        print("  ✓ SetPolylineToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetPolylineToolEntityGroupVisibility: {e}")
    
    try:
        view.SetPolygonToolEntityGroupVisibility(True)
        print("  ✓ SetPolygonToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetPolygonToolEntityGroupVisibility: {e}")
    
    try:
        view.SetRectangleToolEntityGroupVisibility(True)
        print("  ✓ SetRectangleToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetRectangleToolEntityGroupVisibility: {e}")
    
    try:
        view.SetEllipseToolEntityGroupVisibility(True)
        print("  ✓ SetEllipseToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetEllipseToolEntityGroupVisibility: {e}")
    
    try:
        view.SetTrendLineToolEntityGroupVisibility(True)
        print("  ✓ SetTrendLineToolEntityGroupVisibility: True")
    except Exception as e:
        print(f"  ✗ SetTrendLineToolEntityGroupVisibility: {e}")


# =============================================================================
# TOOL MANAGEMENT - ALL 8 TOOL TYPES
# =============================================================================

def demonstrate_text_tool(view, model):
    """Demonstrate Text Tool CRUD operations."""
    print("\n  TEXT TOOL:")
    ref = None
    
    try:
        tool = create_text_tool(model, "Demo Text", "Hello Rosette!", 90, 45)
        ref = view.AddRosetteTextTool(tool)
        print("    ✓ AddRosetteTextTool")
    except Exception as e:
        print(f"    ✗ AddRosetteTextTool: {e}")
    
    try:
        tool_list = view.GetRosetteTextToolList()
        print(f"    ✓ GetRosetteTextToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteTextToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteTextToolVisibility(True)
            print("    ✓ SetRosetteTextToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteTextToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.text = "Updated Text!"
            ref.UpdateRosetteTextTool(value)
            print("    ✓ UpdateRosetteTextTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteTextTool: {e}")
        
        try:
            ref.DeleteRosetteTextTool()
            print("    ✓ DeleteRosetteTextTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteTextTool: {e}")


def demonstrate_arrow_tool(view, model):
    """Demonstrate Arrow Tool CRUD operations."""
    print("\n  ARROW TOOL:")
    ref = None
    
    try:
        tool = create_arrow_tool(model, "Demo Arrow", 0, 0, 90, 45)
        ref = view.AddRosetteArrowTool(tool)
        print("    ✓ AddRosetteArrowTool")
    except Exception as e:
        print(f"    ✗ AddRosetteArrowTool: {e}")
    
    try:
        tool_list = view.GetRosetteArrowToolList()
        print(f"    ✓ GetRosetteArrowToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteArrowToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteArrowToolVisibility(True)
            print("    ✓ SetRosetteArrowToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteArrowToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Arrow"
            ref.UpdateRosetteArrowTool(value)
            print("    ✓ UpdateRosetteArrowTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteArrowTool: {e}")
        
        try:
            ref.DeleteRosetteArrowTool()
            print("    ✓ DeleteRosetteArrowTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteArrowTool: {e}")


def demonstrate_line_tool(view, model):
    """Demonstrate Line Tool CRUD operations."""
    print("\n  LINE TOOL:")
    ref = None
    
    try:
        tool = create_line_tool(model, "Demo Line", 45, 30, 135, 60)
        ref = view.AddRosetteLineTool(tool)
        print("    ✓ AddRosetteLineTool")
    except Exception as e:
        print(f"    ✗ AddRosetteLineTool: {e}")
    
    try:
        tool_list = view.GetRosetteLineToolList()
        print(f"    ✓ GetRosetteLineToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteLineToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteLineToolVisibility(True)
            print("    ✓ SetRosetteLineToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteLineToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Line"
            ref.UpdateRosetteLineTool(value)
            print("    ✓ UpdateRosetteLineTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteLineTool: {e}")
        
        try:
            ref.DeleteRosetteLineTool()
            print("    ✓ DeleteRosetteLineTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteLineTool: {e}")


def demonstrate_polyline_tool(view, model):
    """Demonstrate Polyline Tool CRUD operations."""
    print("\n  POLYLINE TOOL:")
    ref = None
    
    try:
        tool = model.GetDefaultPolylineToolEntityInfo()
        tool.name = "Demo Polyline"
        tool.is_visible = True
        tool.line_format = create_line_format((0, 128, 0), 2)
        tool.points.append(create_vector2d(0.0, 0.0))
        tool.points.append(create_vector2d(0.5, 0.3))
        tool.points.append(create_vector2d(1.0, 0.0))
        ref = view.AddRosettePolylineTool(tool)
        print("    ✓ AddRosettePolylineTool")
    except Exception as e:
        print(f"    ✗ AddRosettePolylineTool: {e}")
    
    try:
        tool_list = view.GetRosettePolylineToolList()
        print(f"    ✓ GetRosettePolylineToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosettePolylineToolList: {e}")
    
    if ref:
        try:
            ref.SetRosettePolylineToolVisibility(True)
            print("    ✓ SetRosettePolylineToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosettePolylineToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Polyline"
            ref.UpdateRosettePolylineTool(value)
            print("    ✓ UpdateRosettePolylineTool")
        except Exception as e:
            print(f"    ✗ UpdateRosettePolylineTool: {e}")
        
        try:
            ref.DeleteRosettePolylineTool()
            print("    ✓ DeleteRosettePolylineTool")
        except Exception as e:
            print(f"    ✗ DeleteRosettePolylineTool: {e}")


def demonstrate_polygon_tool(view, model):
    """Demonstrate Polygon Tool CRUD operations."""
    print("\n  POLYGON TOOL:")
    ref = None
    
    try:
        tool = model.GetDefaultPolygonToolEntityInfo()
        tool.name = "Demo Polygon"
        tool.is_visible = True
        tool.line_format = create_line_format((128, 0, 128), 2)
        tool.points.append(create_vector2d(0.0, 0.0))
        tool.points.append(create_vector2d(0.5, 0.5))
        tool.points.append(create_vector2d(1.0, 0.0))
        ref = view.AddRosettePolygonTool(tool)
        print("    ✓ AddRosettePolygonTool")
    except Exception as e:
        print(f"    ✗ AddRosettePolygonTool: {e}")
    
    try:
        tool_list = view.GetRosettePolygonToolList()
        print(f"    ✓ GetRosettePolygonToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosettePolygonToolList: {e}")
    
    if ref:
        try:
            ref.SetRosettePolygonToolVisibility(True)
            print("    ✓ SetRosettePolygonToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosettePolygonToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Polygon"
            ref.UpdateRosettePolygonTool(value)
            print("    ✓ UpdateRosettePolygonTool")
        except Exception as e:
            print(f"    ✗ UpdateRosettePolygonTool: {e}")
        
        try:
            ref.DeleteRosettePolygonTool()
            print("    ✓ DeleteRosettePolygonTool")
        except Exception as e:
            print(f"    ✗ DeleteRosettePolygonTool: {e}")


def demonstrate_rectangle_tool(view, model):
    """Demonstrate Rectangle Tool CRUD operations."""
    print("\n  RECTANGLE TOOL:")
    ref = None
    
    try:
        tool = model.GetDefaultRectangleToolEntityInfo()
        tool.name = "Demo Rectangle"
        tool.is_visible = True
        tool.line_format = create_line_format((255, 128, 0), 2)
        tool.first_corner = create_vector2d(-0.5, -0.5)
        tool.second_corner = create_vector2d(0.5, 0.5)
        ref = view.AddRosetteRectangleTool(tool)
        print("    ✓ AddRosetteRectangleTool")
    except Exception as e:
        print(f"    ✗ AddRosetteRectangleTool: {e}")
    
    try:
        tool_list = view.GetRosetteRectangleToolList()
        print(f"    ✓ GetRosetteRectangleToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteRectangleToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteRectangleToolVisibility(True)
            print("    ✓ SetRosetteRectangleToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteRectangleToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Rectangle"
            ref.UpdateRosetteRectangleTool(value)
            print("    ✓ UpdateRosetteRectangleTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteRectangleTool: {e}")
        
        try:
            ref.DeleteRosetteRectangleTool()
            print("    ✓ DeleteRosetteRectangleTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteRectangleTool: {e}")


def demonstrate_ellipse_tool(view, model):
    """Demonstrate Ellipse Tool CRUD operations."""
    print("\n  ELLIPSE TOOL:")
    ref = None
    
    try:
        tool = model.GetDefaultEllipseToolEntityInfo()
        tool.name = "Demo Ellipse"
        tool.is_visible = True
        tool.line_format = create_line_format((0, 128, 128), 2)
        tool.first_corner = create_vector2d(-0.3, -0.3)
        tool.second_corner = create_vector2d(0.3, 0.3)
        ref = view.AddRosetteEllipseTool(tool)
        print("    ✓ AddRosetteEllipseTool")
    except Exception as e:
        print(f"    ✗ AddRosetteEllipseTool: {e}")
    
    try:
        tool_list = view.GetRosetteEllipseToolList()
        print(f"    ✓ GetRosetteEllipseToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteEllipseToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteEllipseToolVisibility(True)
            print("    ✓ SetRosetteEllipseToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteEllipseToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated Ellipse"
            ref.UpdateRosetteEllipseTool(value)
            print("    ✓ UpdateRosetteEllipseTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteEllipseTool: {e}")
        
        try:
            ref.DeleteRosetteEllipseTool()
            print("    ✓ DeleteRosetteEllipseTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteEllipseTool: {e}")


def demonstrate_trendline_tool(view, model):
    """Demonstrate TrendLine Tool CRUD operations."""
    print("\n  TREND LINE TOOL:")
    ref = None
    
    try:
        tool = model.GetDefaultTrendLineToolEntityInfo()
        tool.name = "Demo TrendLine"
        tool.is_visible = True
        tool.trend.angle_radians = math.radians(45)
        ref = view.AddRosetteTrendLineTool(tool)
        print("    ✓ AddRosetteTrendLineTool")
    except Exception as e:
        print(f"    ✗ AddRosetteTrendLineTool: {e}")
    
    try:
        tool_list = view.GetRosetteTrendLineToolList()
        print(f"    ✓ GetRosetteTrendLineToolList: {len(tool_list)} found")
    except Exception as e:
        print(f"    ✗ GetRosetteTrendLineToolList: {e}")
    
    if ref:
        try:
            ref.SetRosetteTrendLineToolVisibility(True)
            print("    ✓ SetRosetteTrendLineToolVisibility")
        except Exception as e:
            print(f"    ✗ SetRosetteTrendLineToolVisibility: {e}")
        
        try:
            value = ref.GetValue()
            value.name = "Updated TrendLine"
            ref.UpdateRosetteTrendLineTool(value)
            print("    ✓ UpdateRosetteTrendLineTool")
        except Exception as e:
            print(f"    ✗ UpdateRosetteTrendLineTool: {e}")
        
        try:
            ref.DeleteRosetteTrendLineTool()
            print("    ✓ DeleteRosetteTrendLineTool")
        except Exception as e:
            print(f"    ✗ DeleteRosetteTrendLineTool: {e}")


def demonstrate_all_tools(view, model):
    """Demonstrate all 8 tool types."""
    print("\n" + "=" * 60)
    print("TOOL MANAGEMENT (All 8 Tool Types)")
    print("=" * 60)
    
    demonstrate_text_tool(view, model)
    demonstrate_arrow_tool(view, model)
    demonstrate_line_tool(view, model)
    demonstrate_polyline_tool(view, model)
    demonstrate_polygon_tool(view, model)
    demonstrate_rectangle_tool(view, model)
    demonstrate_ellipse_tool(view, model)
    demonstrate_trendline_tool(view, model)


# =============================================================================
# MAIN
# =============================================================================

def main():
    """Main demonstration function."""
    print("=" * 60)
    print("Dips API - Rosette Tools Demo")
    print("=" * 60)
    
    # Connect to Dips
    print("\nConnecting to Dips application...")
    try:
        app = DipsApp.LaunchApp(62535)
        print("✓ Connected to Dips")
    except Exception as e:
        print(f"✗ Failed to connect: {e}")
        print("Make sure Dips is running with scripting enabled on port 62535")
        return
    
    model = app.GetModel()
    
    # Get or create a Rosette view
    print("\n" + "-" * 60)
    print("VIEW SETUP")
    print("-" * 60)
    views = model.GetRosetteViewList()
    if views:
        view = views[0]
        print(f"  Using existing view: '{view.GetValue().view_name}'")
    else:
        view = model.CreateRosetteView()
        print(f"  Created new view: '{view.GetValue().view_name}'")
    
    # Run demonstrations
    demonstrate_tool_group_visibility(view)
    demonstrate_all_tools(view, model)
    
    # Show the application
    app.Show()
    
    # Summary
    print("\n" + "=" * 60)
    print("DEMONSTRATION COMPLETE")
    print("=" * 60)
    print("""
All Rosette Tool services demonstrated (49 methods):

TOOL GROUP VISIBILITY (9 methods):
  - SetToolsEntityGroupVisibility
  - SetTextToolEntityGroupVisibility
  - SetArrowToolEntityGroupVisibility
  - SetLineToolEntityGroupVisibility
  - SetPolylineToolEntityGroupVisibility
  - SetPolygonToolEntityGroupVisibility
  - SetRectangleToolEntityGroupVisibility
  - SetEllipseToolEntityGroupVisibility
  - SetTrendLineToolEntityGroupVisibility

TOOLS - 8 tool types, 5 methods each (40 methods):
  Tool types: Text, Arrow, Line, Polyline, Polygon, Rectangle,
              Ellipse, TrendLine
  Methods per type:
    - AddRosette[ToolType]Tool
    - GetRosette[ToolType]ToolList
    - UpdateRosette[ToolType]Tool
    - DeleteRosette[ToolType]Tool
    - SetRosette[ToolType]ToolVisibility
""")


if __name__ == "__main__":
    main()