I'm tweaking an old macro of mine that toggles text background on and off for selected elements. It works for text and text nodes; I want it to work for dimensions (and notes) and text in cells.The Cells don't seem too bad - enumerate and run through the contents to modify any that are text/text nodes.
Dimensions appear to be another animal. I cannot tease out how to identify and access the text portion of the dimension. I see how to tell if there IS text, I can see how to read those text strings, but I don't know how/if I can directly modify those pieces of text.
Does anyone have any suggestions or success at this type of thing? Thank you!
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
MaryB
Power GeoPak 08.11.09.918Power InRoads 08.11.09.918OpenRoads Designer 2021 R2
Hi,
Unknown said:Is that just the value of the piece of text, or is it the actual text piece so that I can change the format?
What about to consult MicroStation VBA help file? It contains the answer already:
Unknown said:so that I can change the format?
I am not sure what do you mean by "format".
With regards,
Jan
Mary,
From your description I think you need to be modifying the TextStyle defined in the DimensionStyle to change the text background.
The function below demonstrates how to update the text of a dimension element. You have to iterate through all of the PrimaryText and SecondaryText properties for each segment of the dimension. These are just string values that get their style settings from the TextStyle settings in the DimensionStyle.
Function UpdateDimensionText(strPrefix As String, strSuffix As String) As Long Dim iCount As Long ' index for dimension element array Dim lElem As Long Dim lElemStart As Long Dim lElemEnd As Long ' index for segments in dimension element Dim lSeg As Long Dim lSegStart As Long Dim lSegEnd As Long Dim ee As ElementEnumerator Dim escan As ElementScanCriteria Dim eArray() As Element Dim oDim As DimensionElement Set escan = New ElementScanCriteria escan.ExcludeAllTypes escan.IncludeType msdElementTypeDimension Set ee = ActiveModelReference.Scan(escan) eArray = ee.BuildArrayFromContents lElemStart = LBound(eArray) lElemEnd = UBound(eArray) lSegStart = 1 iCount = 0 For lElem = lElemStart To lElemEnd Set oDim = eArray(lElem).AsDimensionElement iCount = iCount + 1 lSegEnd = oDim.SegmentsCount For lSeg = lSegStart To lSegEnd oDim.PrimaryText(lSeg) = strPrefix & "*" & strSuffix oDim.SecondaryText(lSeg) = "*" oDim.Rewrite Next Set oDim = Nothing Next Set ee = Nothing Set escan = Nothing UpdateDimensionText = iCount End Function
Rod WingSenior Systems Analyst
I'm still trying to write my macro to turn background on for any text-like elements I can select. And it's still not working. Here are a couple of code snippets I have questions for:
Public Function ToggleBackground(Txtstyle As TextStyle) If Txtstyle.BorderAndBackgroundVisible = False Then ' Set Background settings and turn background on With Txtstyle .BackgroundFillColor = ActiveDesignFile.ExtractColorTable.BackColor .BorderColor = ActiveDesignFile.ExtractColorTable.BackColor .BorderMargins = Point2dFromXY(0.5, 0.5) .BorderLineStyle = ActiveDesignFile.LineStyles("0") .BorderLineWeight = ByLevelLineWeight .BorderAndBackgroundVisible = True End With Else Txtstyle.BorderAndBackgroundVisible = False End If End Function
My background is not coming in as the background color from my color table. Not sure what I'm doing wrong there.The margins are HUGE. I tried to set the Point2D to the factor I would normally use in the text style dialog to set my margins, but that's apparently not the answer.
I figured this out. The value that I need for the Point2D is TxtStyle.Height * 0.5. While the dialog box reads the value and applies it to text height as a factor, the Point2D uses the actual value; I need to apply the factor myself.
The background masking is coming in the right color, but the border around the background is still wrong. Any ideas?
And then there are dimensions.
Case msdElementTypeDimension ' Dimension - get textstyle and change background Set oDimStyle = oElement.AsDimensionElement.DimensionStyle ToggleBackground oDimStyle.TextStyle
This gives me "Index not found. The specified index or name does not exist in the collection."My first instinct is that it's trying to tell me that TextStyle is not a property of DimensionStyle. Am I even close?
Thank you for assisting me as I flounder along!
If oDim.PrimaryText is a string, how do I access and change the Text Style?
Hi Marry,
Unknown said:My first instinct is that it's trying to tell me that TextStyle is not a property of DimensionStyle. Am I even close?
I guess it's a bug in VBA API. I found older discussion about how to access text style defined in dimension style (and not even used in any dimension, just to use ActiveDesignReference.DimensionStyles collection) and similar error was reported by a user: It's not possible, because for the TextStyle property MicroStationDGN Level Error 32768 is reported.
Maybe the solution is to use MDL function through VBA wrapper to obtain TextStyle used in DimStyle, but I have not such code example in my pockets.
Thanks!Whether I like the answer or not, it makes sense.
MDL function through VBA wrapper? I may not need that exact example, but could you give me any example of that sort of maneuver? I don't know MDL at all.
Thank you.
Hi Mary,
Unknown said:I don't know MDL at all.
MDL is C in fact, so to acces MDL functions requires to know a bit about this language, C data types, structures and sometimes also pointers.
Unknown said:MDL function through VBA wrapper?
Every (not sure if really every ;-) MDL function has own VBA wrapper that allows to call this function from MDL. VBA wrapper description is part of MDL documentation.
Unknown said:but could you give me any example
I recommend to check "Interacting with MDL" chapter in MicroStation VBA help, where pretty comprehensive overview (and some really good advices) is summarized.