Accessing pressure network

@Drbohlav @Paolo_Emilio_Serra1 I believe I got a quick solution. I quickly edited the python scripts that came with Civil 3D. Will try tidying it up even more later. At least it will give you an idea of how to avoid exporting to excel. Note that it requires you to list the parameters you would like to update in a code block.

# Load the Python Standard and DesignScript Libraries
import sys
import clr
import os
import csv
import json
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib')

# Add Assemblies for AutoCAD and Civil3D
clr.AddReference('AcMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AcDbMgd')
clr.AddReference('AecBaseMgd')
clr.AddReference('AecPropDataMgd')
clr.AddReference('AeccDbMgd')

# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *

# Import references from Civil3D
from Autodesk.Civil.ApplicationServices import *
from Autodesk.Civil.DatabaseServices import *

# Add Assemblies for AutoCAD and Civil 3D APIs
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
clr.AddReference('AeccPressurePipesMgd')
clr.AddReference('acdbmgdbrep')
clr.AddReference('System.Windows.Forms')

# Add references to manage arrays, collections and interact with the user
from System import *
from System.IO import *
from System.Collections.Specialized import *
from System.Windows.Forms import MessageBox

# Create an alias to the Autodesk.AutoCAD.ApplicationServices.Application class
import Autodesk.AutoCAD.ApplicationServices.Application as acapp

# Import references for PropertySets
from Autodesk.Aec.PropertyData import *
from Autodesk.Aec.PropertyData.DatabaseServices import *

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor


obj_handles = IN[1]
pset_name = IN[0]
para_values = IN[2]
para_names = IN[3]

def loin_pset_test(name):
    """Selects an object by handle and assigns the specified property set values.
    XXXXXX

    name: the name of the property set.
    """
    global adoc
    global ed
    db = adoc.Database
    with adoc.LockDocument():
        with adoc.Database as db:
            ####################################################
            dpsd = DictionaryPropertySetDefinitions(db)
            psdef = None
            # Check if the property set exists
            try:
                psdef = dpsd.GetAt(name)
            except Exception as ex:
                MessageBox.Show('Property Set\r\n{}'.format(ex.message))
            if psdef is None:
                return  # Fails gracefully with no property set with that name
            # Safely manage the Database
            ####################################################        
            with db.TransactionManager.StartTransaction() as t:
                # Place your code below
                for idx, val in enumerate(obj_handles[0]):
                    # Get an object by handle
                    hndl = None
                    try:
                        hndl = Handle(Convert.ToInt64(val, 16))
                    except Exception as ex:
                        MessageBox.Show('Handle\n{}\n{}'.format(val, ex.message))
                        return
                    id = None
                    try:
                        id = db.GetObjectId(False, hndl, 0)
                    except Exception as ex:
                        MessageBox.Show('{}'.format(ex.message))
                    if id is None:
                        continue
                    obj = t.GetObject(id, OpenMode.ForWrite)

                    # Check if the object has the psdef associated
                    obj_ps = None
                    try:
                        PropertyDataServices.AddPropertySet(obj, psdef.ObjectId)
                    except Exception as ex:
                        MessageBox.Show('Assign Property Set\r\n{0}\r\n{1}'.format(hndl, ex.message))
                        return
                    for psid in PropertyDataServices.GetPropertySets(obj):
                        obj_ps = t.GetObject(psid, OpenMode.ForWrite)
                        if obj_ps.PropertySetDefinition == psdef.ObjectId:
                            break
                            
                    # Assign the values to the properties
                    for i, parameter in enumerate(para_names):                    
                        try:
                            pid = obj_ps.PropertyNameToId(parameter)
                            prop = [p for p in obj_ps.PropertySetData if p.Id == pid]
                            if len(prop) > 0:
                                prop = prop[0]
                            else:
                                continue
                            value = para_values[i][0][idx]
                            if prop.DataType == DataType.Real:
                                value = float(value)
                            elif prop.DataType == DataType.Integer:
                                value = int(value)
                            elif prop.DataType == DataType.TrueFalse:
                                value = bool(value)
                            elif prop.DataType == DataType.Text:
                                value = str(value)
                            elif prop.DataType == DataType.AlphaIncrement:
                                pass
                            elif prop.DataType == DataType.AutoIncrement:
                                pass
                            elif prop.DataType == DataType.Graphic:
                                pass
                            elif prop.DataType == DataType.List:
                                pass
                            else:
                                pass
                            obj_ps.SetAt(pid, value)
                        except Exception as ex:
                            MessageBox.Show('Assign Property Value\r\n{0}\n{1}'.format(hndl, value))
                            continue
 
                # Commit before end transaction
                t.Commit()
                pass

loin_pset_test(pset_name)
OUT = True

5 Likes