Setting pipe network data source in profile

Hi All

I have created a script that will create a profile view and add selected parts to it.
The only problem I have now is that the Data source containing the selected pipes does not get correctly selected when the profile view gets created. So I want to write a python code to set the pipe source.
Pipe data source selection

I found some code here - https://forums.autodesk.com/t5/civil-3d-customization/profile-view-bands/td-p/4744655
also referenced in another post about setting profile source info - https://forum.dynamobim.com/t/set-profileview-band-profile-1-or-profile-2-to-new-profile/55677/9

I have attempted to edit the code to set the pipe source, but I am quite new at the python node and get the error - unexpected token … I think the error is at line55, but I am not sure how to fix it - any help will be appreciated.

See the python code:
I also attach the script
Create profile view_styles defined.dyn (67.3 KB)

# Load the Python Standard and DesignScript Libraries
import sys
import clr

# 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 *

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
civdoc =CivilApplication.ActiveDocument

def Set_pipesource(pv,pipesource)

	global adoc

	with adoc.LockDocument():
    	with adoc.Database as db:

        	with db.TransactionManager.StartTransaction() as t:
        	#get profile view id
        	pvId =[0].InternalObjectId
        	#Open profile view
        	pvo = pvId.GetObject(OpenMode.ForWrite)
        	#get band set on profile view
        	pvbs = pvo.Bands
        	#get band item properties
        	pvbsi = pvbs.GetBottomBandItems()
        	#count number of bands
        	pvbsc = pvbsi.count
        	pvbscc = pvbsc-1
        	      	
        	#loop through bands, check if pipenetwork type and set datasource
        	for k in range(0,pvbscc):
        		pvbsii =pvbsi.Item[k]
        		
        		If pvbsii.BandType == BandType.Pipenetwork:
        			pvbsii.DataSourceId = [1]
        			
        	pvbsi = pvbs.GetBottomBandItems()
        			
        	return pvbsi
        	
        	
			


pv = IN[0]
pipesource =IN[1]


# Assign your output to the OUT variable.
OUT = Set_pipesource(pv,pipesource)

Hi
Add line

t.Commit()

Thanks @hosneyalaa, I have added the t.commit() line and worked through some subsequent errors and renamed some variables. The script seems to run now, but the existing profile view does not get updated. I initally had the code wrapped in a function, but I would assume the OUT takes care of the output?

Any advice?

Create profile view_styles defined.dyn (69.7 KB)

# list of inputs
profview = IN[0]
pipe = IN[1]

global adoc

with adoc.LockDocument():
    with adoc.Database as db:
        # open a transaction
        with db.TransactionManager.StartTransaction() as t:
            # get the network id for the pipe
            pipe_id = pipe[0].InternalObjectId
            pipesource = pipe_id.NetworkId #[0] to use first item in "list"
            # get profile view id
            profviewId = profview.InternalObjectId
            # Open profile view
            profviewopen = profviewId.GetObject(OpenMode.ForWrite)
            # get band set on profile view
            profview_bandset = profviewopen.Bands
            # get band item properties
            profview_bandset_items = profview_bandset.GetBottomBandItems()
            # count number of bands
            profview_botband_count = profview_bandset_items.Count
            # loop through bands, check if pipenetwork type and set datasource
            for k in range(0, profview_botband_count - 1):
                active_band = profview_bandset_items.Item[k]

                # if statement gives error just set source, i.e. assuming all bands are pipenetwork bands
                # if active_band.BandType == BandType.Pipenetwork:
                # active_band.DataSourceId = pipesource

                active_band.DataSourceId = pipesource

            profview_bandset_items = profview_bandset.GetBottomBandItems()

            t.Commit()
            # return profview_bandset_items - assuming the rCommit is a replacement for return
            pass


# Assign your output to the OUT variable.
OUT = profview

Hi I’m sorry
My language bad

Read the subject you attached , in last subject
I think, you need this method
profview.Bands.SetBottomBandItems(bottomBands)

If not working
Can you attached example drawing

Hi @hosneyalaa - I appreciate your willingness to help. I have looked in more detail at this post: Re: Profile View Bands - Autodesk Community - Civil 3D and it seems like they create a collection of the band id’s , delete the bands, and then re-assign the “corrected” bands…
I have tried to mimic this approach, but I am not sure how to get the BandstyleId attribute.

        for m in range(profview_botband_count):
            band_id = profview_bandset_items[m].BandStyleId
            band_col.append(band_id)

gives me error: AttributeError: BandStyleId

Then I tried adding the inherent form ProfileViewBandSetItem.)

        for m in range(profview_botband_count):
            band_id = ProfileViewBandSetItem[m].BandStyleId
            band_col.append(band_id)

gives me error: NameError: name ‘ProfileViewBandSetItem’ is not defined.

So… I am at the end of my rope…
I attach a sample drawing, code snippet and code.

Set PV band_test drawing.dwg (968.4 KB)
STW_Culverts_Create profile view.dyn (70.7 KB)

BandStyleId this property only set … check it

So you have any errors

band_id = ProfileViewBandSetItem[m].BandStyleId
band_col.append(band_id)

Hi @hosneyalaa - Yes I get this error

hi

@hosneyalaa still no joy

import BandType Enumeration into code

https://help.autodesk.com/view/CIV3D/2022/ENU/?guid=0afb06b2-636c-7d6d-79ee-c06fb33ebb8e

Tried -

import Enumeration BandType

from Autodesk.Civil import BandType

:smiling_face_with_tear: - Sorry, I really don’t understand what I am doing wrong, I re-attach the drawing and script if that helps
Set PV band_test drawing.dwg (985.1 KB)
STW_Culverts_Create profile view.dyn (70.5 KB)

TRY

import sys
import clr
import traceback  # for debugging


# 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 *
from Autodesk.Civil import BandType

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
adoc = Application.DocumentManager.MdiActiveDocument
editor = adoc.Editor
civdoc = CivilApplication.ActiveDocument

# list of inputs
profview = IN[0]
pipe = IN[1]
output = []
global adoc
with adoc.LockDocument():
    with adoc.Database as db:
        # open a transaction
        with db.TransactionManager.StartTransaction() as t:

            pipe_id = pipe.InternalObjectId  # get pipe id from pipe object selected, [0] to use the first item in the "list"
            pipesource = pipe_id.NetworkId  # get the network id from the pipe
            profview_Id = profview.InternalObjectId  # get profile view id
            
            profview_open = profview_Id.GetObject(OpenMode.ForWrite)  # make the profile view editable
            profview_bandset = profview_open.Bands
            profview_bandset_items = profview_bandset.GetBottomBandItems()  # get band item properties
            profview_botband_count = profview_bandset_items.Count  # count the number of bands
            #profview_bandset_items.RemoveAll()  # bandstyleid saved in collection so can remove from profileview
            # loop through band collection, check if pipenetwork type and set datasource
            for band in profview_bandset_items:
                #output.append(band.BandType)
                if band.BandType == BandType.PipeNetwork:
                    band.DataSourceId = pipesource
                    output.append(pipesource)

            profview_bandset.SetBottomBandItems(profview_bandset_items)  # set bands in profile view
            

            t.Commit()
            #pass


# Assign your output to the OUT variable.
OUT =  profview_bandset_items







STW_Culverts_Create profile view(1).dyn (70.9 KB)

Thank you so much for your patience!