# Add Profile Label group from Label Set

**URL:** <https://forum.dynamobim.com/t/add-profile-label-group-from-label-set/106330>\
**Category:** Civil 3D\
**Tags:** python\
**Created:** [December 4, 2024, 3:35pm UTC](https://forum.dynamobim.com/t/add-profile-label-group-from-label-set/106330 "2024-12-04T15:35:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![erik.Jak](https://avatars.discourse-cdn.com/v4/letter/e/ecc23a/32.png) [@erik.Jak](https://forum.dynamobim.com/u/erik.Jak)\
**Post date:** [December 4, 2024, 3:35pm UTC](https://forum.dynamobim.com/t/add-profile-label-group-from-label-set/106330/1 "2024-12-04T15:35:50Z")

</div>

I’m triying to add label groups to a certain profile from a given Label Set already created. I’ve come so far, getting the correct Label Set and iterating each item for creating a new one (i’ve searched on forums and that seems the way to do this).  
For development purposes only, the code below looks for a certain profile view and a specific profile to apply this labels. The idea is to work on something more general once i get the code working.  
The main problem: I cant get the ProfileLineLabelGroup.Create() method to work and i don’t understand why.  
Here’s a forum topic i’ve come across and found useful: [Add Profile Label Set To Profile](https://forums.autodesk.com/t5/civil-3d-customization/add-profile-label-set-to-profile/td-p/12668989)

Hope anybody can help me. Thank you!

```auto
import clr
import sys

# Add references to AutoCAD and Civil 3D assemblies
clr.AddReference('AcMgd') # AutoCAD Managed libraries
clr.AddReference('AcDbMgd')
clr.AddReference('AcCoreMgd')
clr.AddReference('AeccDbMgd') # Civil 3D Managed libraries

# Import necessary namespaces
from Autodesk.AutoCAD.ApplicationServices import Application
from Autodesk.AutoCAD.DatabaseServices import OpenMode
from Autodesk.Civil.ApplicationServices import CivilApplication

# Import .NET System namespace for exception handling
clr.AddReference("System")
from System import Exception

# Get the current document and Civil 3D document
doc = Application.DocumentManager.MdiActiveDocument
db = doc.Database
civilDoc = CivilApplication.ActiveDocument
stylesRoot = civilDoc.Styles

# Input for target label set name
targetLabelSet_Name = IN[0]

# Initialize the output variable for Dynamo
OUT = None

# Single function for encapsulated logic
def process_alignments_and_labels():
    """
    Process alignments, profile views, and labels in one encapsulated logic block.
    """
    global OUT
    try:
        # Lock the document
        lock = doc.LockDocument()

        # Start a transaction
        with db.TransactionManager.StartTransaction() as tr:
            # Step 1: Retrieve target label set by name
            targetLabelSet_Id = None
            labelSetStyles = stylesRoot.get_LabelSetStyles()
            profileLabelSetStyles = labelSetStyles.ProfileLabelSetStyles
            profileLabelSetStylesIds = profileLabelSetStyles.ToObjectIds()

            for styleId in profileLabelSetStylesIds:
                obj = tr.GetObject(styleId, OpenMode.ForRead)
                objName = obj.get_Name()
                if objName == targetLabelSet_Name:
                    targetLabelSet_Id = styleId
                    print(f"Match found! Label Set: {objName}")
                    break

            if not targetLabelSet_Id:
                raise Exception(f"Target label set '{targetLabelSet_Name}' not found.")

            # Step 2: Process alignments and profiles
            alignment_ids = civilDoc.GetAlignmentIds()
            fifth_alignment_id = alignment_ids[4] # Select the 5th alignment
            fifth_alignment = tr.GetObject(fifth_alignment_id, OpenMode.ForRead)
            print(f"Selected Alignment: {fifth_alignment.get_Name()}")

            # Retrieve profile views for the alignment
            profile_view_ids = fifth_alignment.GetProfileViewIds()
            first_profile_view_id = profile_view_ids[0]
            first_profile_view = tr.GetObject(first_profile_view_id, OpenMode.ForRead)
            print(f"Associated Profile View: {first_profile_view.get_Name()}")

            # Process profiles within the alignment
            profile_ids = fifth_alignment.GetProfileIds()
            for profile_id in profile_ids:
                    profile = tr.GetObject(profile_id, OpenMode.ForWrite)
                    profile_name = profile.get_Name()

                    # Check if profile name starts with "PL"
                    if profile_name.startswith("PL"):
                        print(f" Applying Label Set to Profile: {profile_name}")

                        # Apply the label set to the profile
                        labelSetStyle = tr.GetObject(targetLabelSet_Id, OpenMode.ForRead)
                        for labelItem in labelSetStyle:
                            label_type = labelItem.LabelStyleType
                            label_name = labelItem.get_LabelStyleName()
                            label_id = labelItem.get_LabelStyleId()
                            print(f" Label Type: {label_type}")
                            print(f" Label Name: {label_name}")

                            if label_type == 251704338: # Code for Line labels
                                labelItemStyleId = label_id
                                print(first_profile_view_id)
                                print(profile_id)
                                print(labelItemStyleId)
                                label_id = ProfileLineLabelGroup.Create(
                                    first_profile_view_id,
                                    profile_id,
                                    labelItemStyleId
                                )
                            print(f" Created PVI Label Group for {profile_name}")
                    else:
                        continue

            # Commit the transaction
            tr.Commit()

    except Exception as e:
        # Assign the error message to OUT for output in Dynamo
        OUT = f"An error occurred: {e}"
    finally:
        # Ensure the lock is released
        if 'lock' in locals():
            lock.Dispose()

# Execute the workflow
process_alignments_and_labels()

```

---

<div class="post-metadata">

**Author:** ![hosneyalaa](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/hosneyalaa/32/100865_2.png) [@hosneyalaa](https://forum.dynamobim.com/u/hosneyalaa)\
**Post date:** [December 4, 2024, 6:22pm UTC](https://forum.dynamobim.com/t/add-profile-label-group-from-label-set/106330/2 "2024-12-04T18:22:47Z")

</div>

I understand that line  
label\_id = labelItem.get\_LabelStyleId()  
Does it work for you? Must be

label\_id = labelItem.LabelStyleId()

Can you attached example drawing
