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

Change Label Style with Entity Jig

6 REPLIES 6
Reply
Message 1 of 7
heitz15
411 Views, 6 Replies

Change Label Style with Entity Jig

Hey guys,

 

Im developing a code to change the Label Style with EntityJig. When i run this code:

 

 <CommandMethod("Code2")>
        Sub LabelPV2()
            Dim pcC3DDoc As CivilDocument = CivilApplication.ActiveDocument
            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Using pTrans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
                Dim pPipeNetworkId As ObjectId = pcC3DDoc.GetPipeNetworkIds(0)
                Dim pPipeNetwork As Network = pTrans.GetObject(pPipeNetworkId, OpenMode.ForRead)
                Dim pPVId As ObjectId = pPipeNetwork.GetStructureIds(0)
                Dim pPV As [Structure] = pTrans.GetObject(pPVId, OpenMode.ForWrite)

                Dim pStructureLabelIDCollection As ObjectIdCollection = pPV.GetAvailableStructureLabelIds
                Dim oStructureLabel As StructureLabel = pTrans.GetObject(pStructureLabelIDCollection(0), OpenMode.ForWrite)

                Dim pLabelEsquerda As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(0), OpenMode.ForRead)
                Dim pLabelDireita As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(1), OpenMode.ForRead)
                oStructureLabel.StyleName = pLabelDireita.Name

               
                pTrans.Commit()
            End Using
        End Sub

It works fine. The code changes the style of the label. But what i want, is to change the style while dragging the label, so i applied the idea on a EntityJig Code:

 

Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.Civil.DatabaseServices
Imports Autodesk.Civil.DatabaseServices.Styles

Public Class o_vb_Jig
    Inherits EntityJig
    Private pPonto As Point3d
    Property pLabelEsquerda As LabelStyle
    Property pLabelDireita As LabelStyle
    Property pPVLocation As Point3d

    Public Sub New(pLabel As StructureLabel)
        MyBase.New(TryCast(pLabel.Clone(), StructureLabel))
        pPonto = pLabel.LabelLocation
    End Sub

    Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus

        Dim pPPO As New JigPromptPointOptions("Mover Label")
        Dim pPPR As PromptPointResult = prompts.AcquirePoint(pPPO)

        If pPPR.Status <> PromptStatus.OK Then
            Return SamplerStatus.Cancel
        End If
        If pPPR.Value.DistanceTo(pPonto) < Tolerance.Global.EqualPoint Then
            Return SamplerStatus.NoChange
        End If

        pPonto = pPPR.Value
        Return SamplerStatus.OK
    End Function

    Protected Overrides Function Update() As Boolean
        Dim pLabel = DirectCast(Entity, StructureLabel)
        pLabel.LabelLocation = pPonto
        If pPonto.X < pPVLocation.X Then
            pLabel.StyleName = pLabel.StyleName
        Else
            pLabel.StyleName = pLabel.StyleName
        End If

        Return True
    End Function

    Public ReadOnly Property NovoLocal() As Point3d
        Get
            Return pPonto
        End Get
    End Property
End Class

But when it get to this step (the part in red) i got a message error that i dont really know how to solve. Does anyone have any idea how to proceed?

 

Thank you!

 

6 REPLIES 6
Message 2 of 7
Jeff_M
in reply to: heitz15

What is the error you get? Your jig code would not be changing anything, as it is setting the style to the existing style.
Jeff_M, also a frequent Swamper
EESignature
Message 3 of 7
heitz15
in reply to: Jeff_M

program.jpg

 

@Jeff_M, this error right here

Message 4 of 7
Jeff_M
in reply to: heitz15

@heitz15, as I mentioned previously, look at that line of code. You are trying to set the Stylename to the same Stylename. I don't think that C3D's API likes that. So, change the code to be setting the stylename correctly, and also add an If block to only execute when the styles are not the same. Also, in the code you posted I don't see where you are setting the value of the pPVLocation variable.

Jeff_M, also a frequent Swamper
EESignature
Message 5 of 7
diegoal
in reply to: Jeff_M

Hello @Jeff_M

 

@heitz15 and I we work together. The previous code was sent with this error you have pointed.

 

We are trying to develop a code that verifies if the label location is at the strucure (wich we call here as “PV”) left or right side and change its style to left (“esquerda”) or right (“direita”).

We have a subroutine (“TesteLabels”) that calls the label Jig.

 

Sub TesteLabels()

            Dim pcC3DDoc As CivilDocument = CivilApplication.ActiveDocument
            Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor

            Using pTrans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
                Dim pPipeNetworkId As ObjectId = pcC3DDoc.GetPipeNetworkIds(0)
                Dim pPipeNetwork As Network = pTrans.GetObject(pPipeNetworkId, OpenMode.ForRead)
                Dim pPVId As ObjectId = pPipeNetwork.GetStructureIds(0)
                Dim pPV As [Structure] = pTrans.GetObject(pPVId, OpenMode.ForWrite)
                Dim pStructureLabelIDCollection As ObjectIdCollection = pPV.GetAvailableStructureLabelIds
                Dim oStructureLabel As StructureLabel = pTrans.GetObject(pStructureLabelIDCollection(0), OpenMode.ForWrite)

                Dim pLabelEsquerda As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(0), OpenMode.ForRead)
                Dim pLabelDireita As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(1), OpenMode.ForRead)

                Dim pJig As New o_vb_Jig(oStructureLabel)
                pJig.pLabelEsquerda = pLabelEsquerda
                pJig.pLabelDireita = pLabelDireita
                pJig.pPVLocation = pPV.Location

                Dim oPR As PromptResult = ed.Drag(pJig)

                If oPR.Status = PromptStatus.OK Then
                    oStructureLabel.UpgradeOpen()
                    oStructureLabel.LabelLocation = pJig.NovoLocal
                    pTrans.Commit()
                End If
            End Using
        End Sub

 

And here is the label Jig

 

Public Class o_vb_Jig
    Inherits EntityJig
    Private pPonto As Point3d
    Property pLabelEsquerda As LabelStyle
    Property pLabelDireita As LabelStyle
    Property pPVLocation As Point3d

    Public Sub New(pLabel As StructureLabel)
        MyBase.New(TryCast(pLabel.Clone(), StructureLabel))
        pPonto = pLabel.LabelLocation
    End Sub

    Protected Overrides Function Sampler(prompts As JigPrompts) As SamplerStatus

        Dim pPPO As New JigPromptPointOptions("Mover Label")
        Dim pPPR As PromptPointResult = prompts.AcquirePoint(pPPO)

        If pPPR.Status <> PromptStatus.OK Then
            Return SamplerStatus.Cancel
        End If
        If pPPR.Value.DistanceTo(pPonto) < Tolerance.Global.EqualPoint Then
            Return SamplerStatus.NoChange
        End If

        pPonto = pPPR.Value
        Return SamplerStatus.OK
    End Function

    Protected Overrides Function Update() As Boolean
        Dim pLabel = DirectCast(Entity, StructureLabel)
        pLabel.LabelLocation = pPonto
        If pPonto.X < pPVLocation.X Then
            pLabel.StyleName = pLabelEsquerda.Name
        Else
            pLabel.StyleName = pLabelDireita.Name
        End If

        Return True
    End Function

    Public ReadOnly Property NovoLocal() As Point3d
        Get
            Return pPonto
        End Get
    End Property
End Class

 

When we try set “pLabel.StyleName = pLabelEsquerda.Name” we get the mentioned error (“Label Style name doesn’t exist…”) 

But, if we do the same in a single code (“LabelPV2”) without calling Jig it doesn't crash.

 

Sub LabelPV2()
        Dim pcC3DDoc As CivilDocument = CivilApplication.ActiveDocument
        Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Using pTrans As Transaction = acDoc.Database.TransactionManager.StartTransaction()
            Dim pPipeNetworkId As ObjectId = pcC3DDoc.GetPipeNetworkIds(0)
            Dim pPipeNetwork As Network = pTrans.GetObject(pPipeNetworkId, OpenMode.ForRead)
            Dim pPVId As ObjectId = pPipeNetwork.GetStructureIds(0)
            Dim pPV As [Structure] = pTrans.GetObject(pPVId, OpenMode.ForWrite)

            Dim pStructureLabelIDCollection As ObjectIdCollection = pPV.GetAvailableStructureLabelIds
            Dim oStructureLabel As StructureLabel = pTrans.GetObject(pStructureLabelIDCollection(0), OpenMode.ForWrite)

            Dim pLabelEsquerda As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(0), OpenMode.ForRead)
            Dim pLabelDireita As LabelStyle = pTrans.GetObject(pcC3DDoc.Styles.LabelStyles.StructureLabelStyles.LabelStyles(1), OpenMode.ForRead)
            oStructureLabel.StyleName = pLabelDireita.Name

            pTrans.Commit()
        End Using
    End Sub

 

Do you have any idea? What we are doing wrong?

---------------------------------------------------------------------------------------------------------
Diego Álvares
Civil Engineer @ Trento Engenharia Ltda.
www.trentoengenharia.com.br
Autocad Civil 3D 2010 to 2017 - Infrastructure Design Suite
Windows 7 64 i7 4770 @3.40GHz 8GB Ram GeForce GTX 760
Please use the Accept as Solution or Kudo buttons when appropriate
---------------------------------------------------------------------------------------------------------
Message 6 of 7
Jeff_M
in reply to: diegoal

@diegoal@heitz15

 

You might try to change up the Update function like so:

 

        Protected Overrides Function Update() As Boolean
            Dim pLabel = DirectCast(Entity, StructureLabel)
            pLabel.LabelLocation = pPonto
            If pPonto.X < pPVLocation.X Then
                If pLabel.StyleId <> pLabelEsquerda.ObjectId Then
                    pLabel.StyleId = pLabelEsquerda.ObjectId
                End If
            Else
                If pLabel.StyleId <> pLabelDireita.ObjectId Then
                    pLabel.StyleId = pLabelDireita.ObjectId
                End If
            End If

            Return True
        End Function

Bad thing, for me, is that I don't use VB so it's difficult to create a project to test here. But this is similar to what we do in c#.

Jeff_M, also a frequent Swamper
EESignature
Message 7 of 7
diegoal
in reply to: Jeff_M

Thank you @Jeff_M!

 

The code is not finished yet but at least Jig is changing label styles.

 

I know that VB is not the most popular here but for now we can't change.

 

Anyway, we can read C# with no problems.  We will not complain 🙂 Any help will be welcome!

 

Thanks again.

---------------------------------------------------------------------------------------------------------
Diego Álvares
Civil Engineer @ Trento Engenharia Ltda.
www.trentoengenharia.com.br
Autocad Civil 3D 2010 to 2017 - Infrastructure Design Suite
Windows 7 64 i7 4770 @3.40GHz 8GB Ram GeForce GTX 760
Please use the Accept as Solution or Kudo buttons when appropriate
---------------------------------------------------------------------------------------------------------

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

Post to forums  

Rail Community


 

Autodesk Design & Make Report