Referencing a previous post about creating dimension elements in C#: https://communities.bentley.com/products/programming/microstation_programming/f/microstation-programming---forum/112608/c-connect-how-to-create-dimension-elements
I've been trying to create a Note type dimension element, I've successfully been able to create the correct leader for the note using the DimensionType.Note parameter from the code in the article above. I'm trying to figure out how to add text to the note. I see where I can get the textparts if the dimension already exists (and replace the text if it already exists), just can't figure out how to add text.
Below is the code for what I have so far
Symbology symbology = new Symbology(); symbology.Color = SymbologyValue.ColorByLevel; symbology.Weight = SymbologyValue.WeightByLevel; symbology.Style =(int) SymbologyValue.StyleByLevel; FileLevelCache fileLevelCache = Session.Instance.GetActiveDgnFile().GetLevelCache(); LevelHandle levelHandle = fileLevelCache.GetLevelByName(textLevelName, true); DirectionFormatter directionFormatter = new DirectionFormatter(Session.Instance.GetActiveDgnModel()); //following class implemented from BDN article //https://communities.bentley.com/products/programming/microstation_programming/f/microstation-programming---forum/112608/c-connect-how-to-create-dimension-elements CreateDimCallback createDimCallback = new CreateDimCallback(dimensionStyle, textStyle, symbology, levelHandle.LevelId, directionFormatter); DimensionElement dimensionElement = new DimensionElement(Session.Instance.GetActiveDgnModel(), createDimCallback , DimensionType.Note); DPoint3d firstPt = new DPoint3d(0, 0, 0); DPoint3d secondPt = new DPoint3d(100,100,0); DPoint3d thirdPt = new DPoint3d(200,200, 0); dimensionElement.InsertPoint(firstPt, null, dimensionStyle, 0); dimensionElement.InsertPoint(secondPt, null, dimensionStyle, 0); dimensionElement.InsertPoint(thirdPt, null, dimensionStyle, 0); dimensionElement.SetHeight(100); dimensionElement.ApplyDimensionStyle(dimensionStyle, true); TextQueryOptions textQueryOptions = new TextQueryOptions(); textQueryOptions.ShouldIncludeEmptyParts = true; TextPartIdCollection textPartIds = dimensionElement.GetTextPartIds(textQueryOptions); //this method returns no TextPartIds on a new dimensionElement foreach (TextPartId txi in textPartIds) { TextBlock textBlock = dimensionElement.GetTextPart(txi); }
Hi Mike,
Mike Robertson said:I've been trying to create a Note type dimension element
Is it possible to share DGN with intended element? Every time note elements are discussed, I am confused, because historically MicroStation offers a dimension of type note (in VBA it's msdDimTypeNote, but also a tool Place Note that creates complex element Note (persisted as a cell).
Because you mentioned text, I assume the second case is what is discussed, but I'd like to be sure.
In such case, it's about to use NoteCellHeaderElement handler, that is responsible to put all note parts (dimension, text...) together in the right way.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Jan,
Thanks for the reply, first time I've tried attaching a file so hope it comes through.
Below is a screen shot of what is in the file where I manually create the note using the ustn tool and the leaders I've been able to create programmatically.
B1Boring06.dgn
ok, now it's clear it's Note element :-)
As I wrote above, NoteCellHeaderElement will create (if I remember right) the complete element for you. Just create text (TextBlock), prepare DimensionStyle and the leader geometry (points array) and call the class constructor to create the element.
Regards,
Thank Jan
That's getting me closer. Using the code below both the textblock & leader are being created but they appear as 2 seperate elements (I have to add them to the model individually). Looking at a manually placed "note" element, I notice they appear associated with the leader being the "driver" and the text being "driven". Time for me to look into creating an association for the first time!
DgnModel activeModel = Session.Instance.GetActiveDgnModel(); TextBlock textBlock = new TextBlock(textStyle, activeModel); textBlock.AppendText("line one"); Element leaderElement = null; DPoint3d[] leaderPoints = new DPoint3d[3] { firstPt, secondPt, thirdPt }; NoteCellHeaderElement noteCellHeaderElement = new NoteCellHeaderElement(out leaderElement, textBlock, dimensionStyle, Session.Instance.GetActiveDgnModel(), leaderPoints); activeModel.Add(leaderElement); activeModel.Add(noteCellHeaderElement);
Mike Robertson said:but they appear as 2 seperate elements (I have to add them to the model individually).
I do not think so. Only LeaderElement should be added to the model, it adds the cell with text automatically.
Mike Robertson said:Looking at a manually placed "note" element, I notice they appear associated with the leader being the "driver" and the text being "driven".
In my opinion it's in opposite direction: The text (cell) is driver, because when it's moved, the leader end is moved also.
Mike Robertson said:Time for me to look into creating an association for the first time!
Everything is done automatically by the class itself, no extra association has to be created.
The only difference between your example and your code can be, I guess, whether it's "joined" using graphic group or not, because it's not done by the handler.
If I add only the leader element the text does not show up, if I only add the NoteCellHeader the leader doesn't show up.
Below is a screenshot of "Driving" and "Driven" from a manually placed note.
Mike Robertson said:If I add only the leader element the text does not show up, if I only add the NoteCellHeader the leader doesn't show up.
Oops, my memory is not good enough. But to check MicroStationAPI was enough to refresh it ;-)
NoteCellHeaderElement.AddToModel(Element, DgnModel)
Sometimes it's worth to read documentation. And it's part of NET doc also, plus offered by VS editor.
In the case it's not clear enough:
noteCellHeaderElement.AddToModel(leaderElement, activeModel);
Thanks Jan, that did what I needed.
Little strange it requires 2 different methods that "out" my leaderElement but it works.
DgnModel activeModel = Session.Instance.GetActiveDgnModel(); TextBlock textBlock = new TextBlock(textStyle, activeModel); textBlock.AppendText("My Note"); Element leaderElement = null; DPoint3d[] leaderPoints = new DPoint3d[3] { firstPt, secondPt, thirdPt }; //3 pts for leader already defined NoteCellHeaderElement noteCellHeaderElement = new NoteCellHeaderElement(out leaderElement, textBlock, dimensionStyle, activeDgnModel, leaderPoints); noteCellHeaderElement.AddToModel(out leaderElement, dgnModel);
Answer Verified By: Mike Robertson