AutoCAD Plant 3D Forum
Welcome to Autodesk’s AutoCAD Plant 3D Forums. Share your knowledge, ask questions, and explore popular AutoCAD Plant 3D topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python for Plant 3D 2023

3 REPLIES 3
Reply
Message 1 of 4
Paul.Carson4JXY2
250 Views, 3 Replies

Python for Plant 3D 2023

I'm interested in pulling some information from a plant 3d drawing with python. So far I've been able to extract a few properties.

  • ObjectName: (AcPpDb3dPipe)
  • Service
  • InsulationType
  • InsulationThickness
  • PnPClassName
  • Layer

I'm really interested in the position x, y, z coordinates and center of gravity but cannot seem to get that information. I've tried a bunch of property names but they aren't working.

Is there a way to find out the property names available in Plant3D itself? I've checked the usual property menu when selecting a pipe but that didn't work.

Is this a restriction of Python not being .NET - if so is there a workaround for this with still using Python if possible?

 

It seems like it's so close because its picking up some properties but not all.

from pyautocad import Autocad
import comtypes.client

def get_all_properties(obj):
    return [prop for prop in dir(obj) if not prop.startswith('__')]

def safe_get_property(obj, prop_name):
    try:
        value = getattr(obj, prop_name)
        if isinstance(value, (tuple, list)) and len(value) == 3:
            return f"({value[0]}, {value[1]}, {value[2]})"
        return str(value)
    except Exception as e:
        return None


def explore_pipe_properties(pipe):
    properties = {}
    prop_list = [
        'ObjectName', 'Layer', 'Length', 'NominalDiameter', 'Material', 'LineNumber',
        'Service', 'InsulationType', 'InsulationThickness',
        'Position', 'EndPosition', 'CenterOfGravity',
        'Specification', 'Rating', 'OutsideDiameter', 'InsideDiameter', 'FlowDirection',
        'Temperature', 'Pressure', 'PaintColor', 'Slope', 'NB', 'PnPClassName', 'Position_X', 'PipeSpec'
    ]

    for prop in prop_list:
        value = safe_get_property(pipe, prop)
        if value is not None:
            properties[prop] = value

    return properties


# Connect to the running instance of AutoCAD
acad = Autocad()

if acad.doc:
    print(f"Connected to: {acad.doc.Name}")

    try:
        model_space = acad.model
        pipes = []

        for obj in model_space:
            obj_name = safe_get_property(obj, 'ObjectName')
            if obj_name == 'AcPpDb3dPipe':
                pipes.append(explore_pipe_properties(obj))


            if len(pipes) >= 100:
                break

        print(f"\nTotal AcPpDb3dPipe objects found: {len(pipes)}")

        if pipes:
            print("\nProperties found on AcPpDb3dPipe objects:")
            for prop, value in pipes[0].items():
                print(f"  {prop}: {value}")

            print("\nSample data for first 5 pipes:")
            for i, pipe in enumerate(pipes[:5], 1):
                print(f"\nPipe {i}:")
                for prop, value in pipe.items():
                    print(f"  {prop}: {value}")
        else:
            print("No AcPpDb3dPipe objects found in the drawing.")
    except Exception as e:
        print(f"An error occurred: {str(e)}")
        print("If the error persists, you may need to check your AutoCAD Plant 3D settings.")
else:
    print("Failed to connect to AutoCAD")

 

3 REPLIES 3
Message 2 of 4
h_eger
in reply to: Paul.Carson4JXY2

Dear @Paul.Carson4JXY2 ,

 

Python is only very rudimentarily supported in AutoCAD Plant 3D .
Therefore there is the SDK for AutoCAD Plant 3D

https://aps.autodesk.com/developer/overview/autocad-plant-3d-and-pid

-

If my reply was helpful, please give a "Kudo" or click the "Accept as Solution" button below (or both).

Hartmut Eger
Senior Engineer
Anlagenplanung + Elektotechnik
XING | LinkedIn

EESignature



Message 3 of 4
Paul.Carson4JXY2
in reply to: h_eger

Ok thanks for the resource. Is that all in C# or is there VB or other languages to implement as well?

 

The end goal would be to have a button within Plant 3D preferably that would run a script and maybe connect to a sharepoint file to compare data.

Message 4 of 4
jabowabo
in reply to: Paul.Carson4JXY2


@Paul.Carson4JXY2 wrote:

Ok thanks for the resource. Is that all in C# or is there VB or other languages to implement as well?


The .NET API can use C# or VB.net languages. You'll find much more help and sample code with C#. 

 


@Paul.Carson4JXY2 wrote:

The end goal would be to have a button within Plant 3D preferably that would run a script and maybe connect to a sharepoint file to compare data.


.NET would likely be the best tool for this. A little outside of the box, but if you really wanted to use Python, you could probably get the info you want from the SQL database.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report