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!
@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.
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?
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#.
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.