Make2D by ghpython

Hi everyone,
I want to use component make2D ghpython, it seems not to work

Hi @Shank,

Here’s a python component I made for this exact purpose, it’s obviously not short and I’m sure could be improved greatly but perhaps it will help you.

You can leave it to run in real-time or use a Boolean for manual refreshing.
It also outputs the Visible Lines/Hidden Lines separately and outputs the computation time

Hopefully it will help.


__author__ = "Michael Vollrath"
__version__ = "2023.07.15"
#Made With ♥ & 🐍 In Dallas, TX

#Set Component Message
if A:
    ghenv.Component.Message = "Real-Time"
else:
    ghenv.Component.Message = "Manual"

ghenv.Component.Name = "Hidden Line Drawing"
ghenv.Component.NickName = "HLD"
ghenv.Component.Description = "Create a hidden line drawing of the input geometry from the input views and optionally run as real-time or require manual refresh"

ghenv.Component.Params.Input[0].Name = "Activate"
ghenv.Component.Params.Input[0].NickName = "A"
ghenv.Component.Params.Input[0].Description = "Set True To Activate"

# ghenv.Component.Params.Input[1].Name = "Tolerance"
# ghenv.Component.Params.Input[1].NickName = "T"
# ghenv.Component.Params.Input[1].Description = "Tolerance For Make2D Operation"

ghenv.Component.Params.Input[1].Name = "Model Object"
ghenv.Component.Params.Input[1].NickName = "O"
ghenv.Component.Params.Input[1].Description = "Objects To Create Hidden Line Drawing From"

ghenv.Component.Params.Input[2].Name = "View"
ghenv.Component.Params.Input[2].NickName = "V"
ghenv.Component.Params.Input[2].Description = "View To Create From"

ghenv.Component.Params.Input[3].Name = "Manual"
ghenv.Component.Params.Input[3].NickName = "M"
ghenv.Component.Params.Input[3].Description = "Enable For Manual Refresh"

ghenv.Component.Params.Output[0].Name = "Curve"
ghenv.Component.Params.Output[0].NickName = "Cv"
ghenv.Component.Params.Output[0].Description = "Visible Curves Resulting From Hidden Line Drawing"

ghenv.Component.Params.Output[1].Name = "Curve"
ghenv.Component.Params.Output[1].NickName = "Ch"
ghenv.Component.Params.Output[1].Description = "Hidden Curves Resulting From Hidden Line Drawing"

ghenv.Component.Params.Output[2].Name = "Execution Time"
ghenv.Component.Params.Output[2].NickName = "T"
ghenv.Component.Params.Output[2].Description = "Time It Took For The Hidden Line Drawing Computation"

import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import scriptcontext as sc
import ghpythonlib.treehelpers as th
import time

# Get the instance ID of the current component
instance_id = str(ghenv.Component.InstanceGuid)

# Check if the custom dictionary exists in scriptcontext.sticky
if "HLD_Attributes" not in sc.sticky:
    # Create a custom dictionary to store the created_curves dictionary
    sc.sticky["HLD_Attributes"] = {}

# Retrieve the created_curves dictionary from the custom dictionary
created_curves = sc.sticky["HLD_Attributes"].get(instance_id, {})

# Check if A or M is set to true, then update the sticky dictionary
if A or M:
    start_time = time.time()

    # Set up a new hidden-line-drawing parameter
    hld_parm = rg.HiddenLineDrawingParameters()
    hld_parm.IncludeTangentEdges = False
    hld_parm.IncludeTangentSeams = False
    hld_parm.Flatten = True
    hld_parm.IncludeHiddenCurves = True

    for geo in O:
        hld_parm.AddGeometry(geo, '')

    Cv = []
    Ch = []

    for view in V:
        hld_parm.SetViewport(view)

        # Compute
        hld = rg.HiddenLineDrawing.Compute(hld_parm, True)

        vis = rg.HiddenLineDrawingSegment.Visibility.Visible
        hid = rg.HiddenLineDrawingSegment.Visibility.Hidden

        view_Cv = []
        view_Ch = []

        for hld_crv in hld.Segments:
            segment_curve = hld_crv.CurveGeometry

            if segment_curve in created_curves:
                crv = created_curves[segment_curve]
            else:
                crv = segment_curve.DuplicateCurve()
                created_curves[segment_curve] = crv

            if hld_crv.SegmentVisibility == vis:
                view_Cv.append(crv)
            elif hld_crv.SegmentVisibility == hid:
                view_Ch.append(crv)
            else:
                pass

        Cv.append(view_Cv)
        Ch.append(view_Ch)

    # Store the Cv and Ch data in the created_curves dictionary
    created_curves["Cv"] = Cv
    created_curves["Ch"] = Ch

    # Update the custom dictionary in scriptcontext.sticky
    sc.sticky["HLD_Attributes"][instance_id] = created_curves

    end_time = time.time()
    execution_time = round(end_time - start_time, 1)
    T = str(execution_time) + "s"

else:
    # Check if the current component's curves are already stored in the dictionary
    if "Cv" in created_curves and "Ch" in created_curves:
        Cv = created_curves["Cv"]
        Ch = created_curves["Ch"]
    else:
        Cv = []
        Ch = []

# Output the curves as lists of curves
Cv_output = Cv
Ch_output = Ch

Cv = th.list_to_tree(Cv_output)
Ch = th.list_to_tree(Ch_output)

20240717_GH_Python_Hidden_Line_Drawing_Via_Make_2D_01a.gh (8.7 KB)

5 Likes

Wow, Thank you.

You’re welcome! :grin:

1 Like

It is this script working with Rhino7? It is not running on my system.

Make2D_GhPython.gh (9.2 KB)

Hi @Cumberland ,

The script component is R8. The code should work on R7 mostly if not entirely. Slight formatting may be needed here and there but you should be able to use the code in the R7 script component if you copy and paste it in and set up your inputs and such.

Hi,
Can we get visible index and hidden index ?