Read PDF Layers in Grasshopper (Python)

Hello,
I would like to modify the attached Python script to read PDF layers rather than 3DM layers, which is it currently set up to do. I tried modifying the Python code but was unable to figure it out. Can anyone help out? Thanks.

From this discussion:
How to read layer names of a remote 3dm file imported/referenced in the current GH file?)

Is this also going to be in Grasshopper? GH in Rhino 8 has new datatypes, so this is much easier.

Thanks @scottd! I’ve experimented with Rhino 8 and saw this functionality existed but didn’t have a chance to try it - thanks for the heads up! I’m still using Rhino 7, so this might be a great reason to upgrade to 8!

Brian

@scottd I had a follow up question. Do you know of a way to select the geometry from a particular layer using these new components? When I tried to filter my selection based on particular layer, it still seemed to still select every object. See attached. Any help would be appreciated!

Import PDF_Rhino8.gh (11.1 KB)
Sample PDF.pdf (2.0 KB)

What specifically should the selection be made on? We can get down to layer, but is there a specific curve on that layer?

Here are the general ways to group and filter the elements with the new tools:

But if there is a specific filter that is needed, we can both give it a try.

Here is an example walking thru the layers:

FilterByPDFLayer.gh (11.6 KB)

1 Like

Initially the questions was surrounding how to read in a PDF with Python. Here is code to read in a PDF to a headless doc that can be manipulated and potentially written back out:

#! python3

import rhinoscriptsyntax as rs
import scriptcontext as sc
import math

import System
import System.Collections.Generic
import Rhino
import os
 
doc = Rhino.RhinoDoc.CreateHeadless(None)
options = Rhino.FileIO.FilePdfReadOptions()
options.LoadText = False

#prompt the user for a file to import
filter = "PDF file (*.pdf)|*.pdf||"
path = rs.OpenFileName("Open PDF File", filter)

doc.Import(path, options.ToDictionary())
 
print(f"{doc.Objects.Count} objects")
for obj in doc.Objects:
    nlayer = obj.Attributes.LayerIndex
    print(f"Layer Index is {nlayer}")
 
doc.Dispose()

More can be found in this guide: Rhino - Code-Driven File IO

Thanks @scottd this works!