How to "Maintain Source Layers" in a Make2D script - HiddenLineDrawing

I’m trying to write a batch Make2D script for all detail viewports on selected layouts that maintains object layers. Just noticed that HiddenLineDrawingParameters.AddGeometry method takes objects’ geometry as an argument and not the rhino objects themselves, which makes me think that layer information (and other object properties) isn’t included anywhere in the resulting HiddenLineDrawing object - in other words it’s lost.

However, in Rhino app there is a “maintain source layers” option. Is there any way to do the same via a python script?

image

WIP:

from Rhino.DocObjects import ObjectType
from Rhino.Geometry import Point2d, HiddenLineDrawing, HiddenLineDrawingParameters
from Rhino.Display import RhinoPageView
import scriptcontext as sc
import rhinoscriptsyntax as rs

selected_page_names = rs.MultiListBox([i.PageName for i in sc.doc.Views.GetPageViews()])
selected_pages = [i for i in sc.doc.Views.GetPageViews() if i.PageName in selected_page_names]

for selected_page in selected_pages:
    for detail_view in RhinoPageView.GetDetailViews(selected_page):
        
        first_corner = Point2d(detail_view.Viewport.Bounds.Left, detail_view.Viewport.Bounds.Bottom)
        second_corner = Point2d(detail_view.Viewport.Bounds.Right, detail_view.Viewport.Bounds.Top)
        
        detail_view.IsActive = True
        
        rhino_objects = sc.doc.Objects.FindByCrossingWindowRegion(detail_view.Viewport, 
                                                                first_corner, second_corner, 
                                                                True, ObjectType.AnyObject)
        
        parameters = HiddenLineDrawingParameters()
        parameters.AbsoluteTolerance = sc.doc.ModelAbsoluteTolerance
        parameters.Flatten = True
        parameters.IncludeHiddenCurves = False
        parameters.IncludeTangentEdges = False
        parameters.IncludeTangentSeams = False
        parameters.SetViewport(detail_view.Viewport)
        for obj in rhino_objects:
            parameters.AddGeometry(obj.Geometry, None)
        
        hld = HiddenLineDrawing.Compute(parameters, True)
        
        for hld_curve in hld.Segments:
            sc.doc.Objects.AddCurve(#WIP here...)

found this example that seems to show how to do that

the third argument to HiddenLineDrawingParameters.AddGeometry method is an object tag, which I assume can be later retrieved to copy source object properties :upside_down_face: