As CAD programmer, we obviously can help CAD users in this situation, so that they do not have to manually rotate the labels in all Viewports with different twisted angles; instead we can write some code to get it done easily.
Here is a short video clip that shows 2 dynamic blocks being used as label: one a block that can be dragged to rotate as whole; the other is a label with a leader (with leader pointer as the block's insertion point), so that the label portion can be dragged to different position and rotated with the leader pointer remains in original insertion point.
This picture shows how the label blocks are inserted into ModelSpace:
And this picture shows how the label blocks look like in a Viewport with twisted angle in paperSpace layout:
It would be ideal that the labels seen via the Viewport are all rotated so that its text information is presented horizontally. This is where the rotation parameter/action in the dynamic block comes into play. Of course, for the label that does not have a pointing leader, we can simply rotate the block reference itself instead of adding a rotation dynamic property. But here I just want to use it in comparison to the dynamic block that has its rotation part at the end of a pointing leader - there is something tricky to handle. Read on.
Firstly, I need to know which blocks in drawing are label blocks, and the dynamic property names I need to set. Here is a class to provide this information:
using Autodesk.AutoCAD.Geometry; using System.Collections.Generic; namespace LabelRotation { public class LabelBlockConfiguration { public static Dictionary<string, Point2d> GetLabelBlockConfigurations() { var dic = new Dictionary<string, Point2d>(); dic.Add("MYTAG", Point2d.Origin); dic.Add("MYLABEL", new Point2d(0.0, 10.0)); return dic; } public const string LABEL_POSITION_PROP = "LabelPosition"; public const string LABEL_ANGLE_PROP = "LabelAngle"; public const string TAG_ANGLE_PROP = "TagAngle"; } }
The data in this class tells me:
1. There are 2 blocks ("MYTAG" and "MYLABEL") used as label;
2. Each block has a dynamic property named as "xxxxAngle", used to set the label's rotation;
3. Block "MYLABEL" also has a dynamic property names as "LabelPosition". Since the this dynamic property is a Point parameter/Stretch action, the actually property name should be "LabelPosition X" and LabelPosition Y" respectively.
4. Each block name is associated with a Point2d value, which is the original value of "LabelPosition" property (for the Point parameter/Stretch action). Why do we need to know this value? We'll see it later that how it is critical to rotate label correctly if the label's rotation part is stretch-able as previous video clip shows.
Here is the code with a command defined to ease CAD users from having to manually rotate the labels according to the twist angle of Viewport:
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using System.Collections.Generic; using CadApp = Autodesk.AutoCAD.ApplicationServices.Application; [assembly: CommandClass(typeof(LabelRotation.Commands))] namespace LabelRotation { public class Commands { private Dictionary<string, Point2d> _labelBlockConfigs = LabelBlockConfiguration.GetLabelBlockConfigurations(); [CommandMethod("SetLabel", CommandFlags.NoTileMode)] public void SetLabelRotationByViewport() { var doc = CadApp.DocumentManager.MdiActiveDocument; var ed = doc.Editor; ed.SwitchToPaperSpace(); try { var lblIds = GetLabelBlocksInModelSpace(doc); double twistAngle; if (GetTwistAngleOfViewportOnLayout(ed, out twistAngle)) { RotateLabels(doc, lblIds, twistAngle); } else { ed.WriteMessage("\n*Cancel*"); } } catch (System.Exception ex) { ed.WriteMessage("\nError\n{0}", ex.Message); ed.WriteMessage("\n*Cancel*"); } finally { Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt(); } } #region private methods private IEnumerable<ObjectId> GetLabelBlocksInModelSpace(Document dwg) { var lblIds = new List<ObjectId>(); var filterVals = new TypedValue[] { new TypedValue((int)DxfCode.Start, "INSERT"), new TypedValue((int)DxfCode.LayoutName, "MODEL") }; var res = dwg.Editor.SelectAll(new SelectionFilter(filterVals)); if (res.Status == PromptStatus.OK) { using (var tran = dwg.TransactionManager.StartTransaction()) { foreach (var id in res.Value.GetObjectIds()) { var blk = (BlockReference)tran.GetObject( id, OpenMode.ForRead); var blkName = blk.Name; if (blk.IsDynamicBlock) { var br = (BlockTableRecord)tran.GetObject( blk.DynamicBlockTableRecord, OpenMode.ForRead); blkName = br.Name; } if (IsTargetLabelBlock(blkName)) { lblIds.Add(id); } } tran.Commit(); } } return lblIds; } private bool IsTargetLabelBlock(string blkName) { foreach (var item in _labelBlockConfigs) { if (item.Key.ToUpper() == blkName.ToUpper()) return true; } return false; } private bool GetTwistAngleOfViewportOnLayout( Editor ed, out double twistAngle) { twistAngle = 0.0; ObjectId vportId = SelectViewport(ed); if (!vportId.IsNull) { using (var tran = vportId.Database.TransactionManager.StartTransaction()) { var vport = (Viewport)tran.GetObject( vportId, OpenMode.ForRead); twistAngle = vport.TwistAngle; tran.Commit(); } return true; } else { return false; } } private ObjectId SelectViewport(Editor ed) { var opt = new PromptEntityOptions( "\nSelect viewport:"); opt.SetRejectMessage("\nInvalid: not a Viewport!"); opt.AddAllowedClass(typeof(Viewport), true); var res = ed.GetEntity(opt); if (res.Status== PromptStatus.OK) { return res.ObjectId; } else { return ObjectId.Null; } } private void RotateLabels( Document dwg, IEnumerable<ObjectId> lblIds, double twistAngle) { double angle = 0.0 - twistAngle; using (var tran = dwg.TransactionManager.StartTransaction()) { foreach (var labelId in lblIds) { var blk = (BlockReference)tran.GetObject( labelId, OpenMode.ForWrite); foreach (DynamicBlockReferenceProperty prop in blk.DynamicBlockReferencePropertyCollection) { if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_ANGLE_PROP.ToUpper() || prop.PropertyName.ToUpper() == LabelBlockConfiguration.TAG_ANGLE_PROP.ToUpper()) { prop.Value = angle; break; } } } tran.Commit(); } } #endregion } }
Now, from this video clip we can see the result of the code execution, which is as expected: all the labels have been rotated into horizontal position according to the twist angle of the Viewport. Well... until this happens: if the stretch-able label portion of the dynamic block has been dragged before the code is executed. See this video clip. If you see the video clip closely, you should be able to see in the "Properties" window that even I do not rotate the label (the "LabelAngle" dynamic property value is not changed initially), simply dragging the label (thus the "LabelPosition X/Y" dynamic property value changes) makes the "LabelAngle" property change its value. That is why the code works only with the label blocks with its original Stretch action position, but not with the labels that were dragged prior to the code execution.
While I now know the Rotation parameter of the dynamic block somehow changes when being used in conjunction with Point parameter/Stretch action, figuring out what the Rotation property value is due to the stretch looks like difficult thing to do. But without knowing the initial rotation angle of a stretched label, my code simple does not work as expected. I was struggling to get my code work in this scenario for a while and a bell suddenly rang in my mind: I could try to save the current Stretch action's position (the property "LabelPosition X/Y", and then reset the property back to its original value (as the dynamic block definition defines); then I rotate the label (setting "LabelAngle" property according to Viewport's twist angle); after the rotation, I restore the label's Stretch action position. So, I modified the code in private method RotateLabels(), as following:
private void RotateLabels( Document dwg, IEnumerable<ObjectId> lblIds, double twistAngle) { double angle = 0.0 - twistAngle; using (var tran = dwg.TransactionManager.StartTransaction()) { foreach (var labelId in lblIds) { var blk = (BlockReference)tran.GetObject( labelId, OpenMode.ForWrite); var blkName = blk.Name; if (blk.IsDynamicBlock) { var br = (BlockTableRecord)tran.GetObject( blk.DynamicBlockTableRecord, OpenMode.ForRead); blkName = br.Name; } Point2d originalPosition = Point2d.Origin; if (_labelBlockConfigs.ContainsKey(blkName.ToUpper())) { originalPosition = _labelBlockConfigs[blkName.ToUpper()]; // Since dynamic property value from linear or point // parameteris affected by block insertion scale, // thus we need to multiply the scale. // Also, for the dynamic block, the block scale must // be uniform scale double scale = blk.ScaleFactors.X; originalPosition = new Point2d( originalPosition.X * scale, originalPosition.Y * scale); } //Save existing label position Point2d currentPosition = GetPositionProperty(blk); //Set label to its original position if (originalPosition!=currentPosition) { SetPositionProperty(blk, originalPosition); } //Set rotation SetRotationProperty(blk, angle); //Restore the label position if (originalPosition != currentPosition) { SetPositionProperty(blk, currentPosition); } } tran.Commit(); } } private Point2d GetPositionProperty(BlockReference blk) { double x = 0.0; double y = 0.0; foreach (DynamicBlockReferenceProperty prop in blk.DynamicBlockReferencePropertyCollection) { if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_POSITION_PROP.ToUpper() + " X") { x = Convert.ToDouble(prop.Value); } if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_POSITION_PROP.ToUpper() + " Y") { y = Convert.ToDouble(prop.Value); } } return new Point2d(x, y); } private void SetPositionProperty(BlockReference blk, Point2d position) { foreach (DynamicBlockReferenceProperty prop in blk.DynamicBlockReferencePropertyCollection) { if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_POSITION_PROP.ToUpper() + " X") { prop.Value = position.X; } if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_POSITION_PROP.ToUpper() + " Y") { prop.Value = position.Y; } } } private void SetRotationProperty(BlockReference blk, double angle) { foreach (DynamicBlockReferenceProperty prop in blk.DynamicBlockReferencePropertyCollection) { if (prop.PropertyName.ToUpper() == LabelBlockConfiguration.LABEL_ANGLE_PROP.ToUpper() || prop.PropertyName.ToUpper() == LabelBlockConfiguration.TAG_ANGLE_PROP.ToUpper()) { prop.Value = angle; break; } } }
With this code change, this video clip shows the labels now are all rotated correctly whether they have been dragged from their original position or not prior to being rotated.
Some discussion:
1. Often there could be multiple Viewports to show different portion of ModelSpace content and the Viewports could have different twist angle. In this case, we obviously need to determine which labels are visible in which Viewport and then rotate them accordingly. In this article I omitted this scenario and simplified the case that all labels are seen in single twisted Viewport.
2. Since I somehow found the way to rotate the label correctly, I did not bother to figure out why/how the rotation property value changes due the dragging action. Because I need to rest the stretched position back to the original position (the position when the dynamic block is defined) before doing the rotation, that is why we need to associate a Point2d value with the block name as known label block information in class LabelBlockConfiguration.
Hey very nice blog!!..
ReplyDeleteBest Autocad training center
best Civil Cadd courses in Lucknow
SolidWorks training
staad pro training
INTERESTING!!
ReplyDeleteGOOD CONTENT
CAD to BIM conversion in Uk
Hi Norman,
ReplyDeleteIs there a way to modify the stretch actions of a dynamic block using the API?
Thanks,
There is no API available to create/modify dynamic properties (parameters, actions) in block definition.
ReplyDelete