使用JIG旋转属性块时属性文字实体保持水平不变
今天看到NEWER版主写了个XDDRX API的 JIG旋转属性快文字保持不变的帖子,我这有个C#的代码,贴出来和大家分享。**** Hidden Message *****
public class BRotateJig : DrawJig
{
public Point3d _insPoint;
public double _brefAngle = 0.0;
private ObjectId _brefId = ObjectId.Null;
private ObjectId _btrId = ObjectId.Null;
private Extents3d _attExts = new Extents3d();
private Dictionary<String, Vector3d> _attDisps;
public Dictionary<String, Point3d> _attPos;
public BRotateJig(ObjectId brefId)
{
// Id of the block reference that we will jig
_brefId = brefId;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockReference bref = tr.GetObject(
brefId, OpenMode.ForWrite) as BlockReference;
_btrId = bref.BlockTableRecord;
_insPoint = bref.Position;
_brefAngle = bref.Rotation;
// We want to determine the extents of the
// attributes without considering the rotation
// that the block reference might already have.
bref.Rotation = 0.0;
// Determines the overall extents of the attribute
// references in the block
BlockTableRecord btr = tr.GetObject(
_btrId, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in btr)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef
= obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
using (AttributeReference attRef
= new AttributeReference())
{
attRef.SetAttributeFromBlock(
attDef, bref.BlockTransform);
Extents3d exts = attRef.GeometricExtents;
_attExts.AddExtents(exts);
}
}
}
// Stores the displacement of each attribute
//reference with mid point as the reference
// This is needed to keep the attribute references
// straight after the block reference is rotated.
Point3d midPt = _attExts.MinPoint +
(_attExts.MaxPoint - _attExts.MinPoint) * 0.5;
_attDisps = new Dictionary<string, Vector3d>();
foreach (ObjectId id in btr)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef
= obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
using (AttributeReference attRef
= new AttributeReference())
{
attRef.SetAttributeFromBlock(
attDef, bref.BlockTransform);
_attDisps.Add(attRef.Tag,
attRef.Position - midPt);
}
}
}
// Stores the positions of the attributes in the
// rotated state.
_attPos = new Dictionary<string, Point3d>();
// The block reference rotation was set to 0.0
// for calculating extents.
// We do not want to save that change
//tr.Commit();
}
}
public bool DoIt()
{
Editor ed
= Application.DocumentManager.MdiActiveDocument.Editor;
PromptResult jigRes = null;
jigRes = null;
jigRes = ed.Drag(this);
if (jigRes.Status != PromptStatus.OK)
return false;
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptAngleOptions jigOpts = new JigPromptAngleOptions();
jigOpts.UserInputControls =
(UserInputControls.Accept3dCoordinates |
UserInputControls.NullResponseAccepted);
jigOpts.Message = "Specify rotation :";
jigOpts.BasePoint = _insPoint;
jigOpts.UseBasePoint = true;
jigOpts.Cursor = CursorType.RubberBand;
PromptDoubleResult jigRes = prompts.AcquireAngle(jigOpts);
double angle = jigRes.Value;
if (_brefAngle == angle)
return SamplerStatus.NoChange;
_brefAngle = angle;
if (jigRes.Status == PromptStatus.OK)
return SamplerStatus.OK;
return SamplerStatus.Cancel;
}
protected override bool WorldDraw(
Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
using (BlockReference bref
= new BlockReference(_insPoint, _btrId))
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr
= db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = tr.GetObject(
_btrId, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId id in btr)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef
= obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
using (AttributeReference attRef
= new AttributeReference())
{
attRef.SetAttributeFromBlock
(attDef, bref.BlockTransform);
Point3d midPt = _attExts.MinPoint
+ (_attExts.MaxPoint-_attExts.MinPoint)
* 0.5;
Vector3d dir = midPt - bref.Position;
dir = dir.RotateBy(
_brefAngle, Vector3d.ZAxis);
midPt = bref.Position.TransformBy
(Matrix3d.Displacement(dir));
if (_attDisps.ContainsKey(attRef.Tag))
{
Point3d attPos =
midPt.TransformBy(
Matrix3d.Displacement(
_attDisps));
attRef.Position = attPos;
if (_attPos.ContainsKey(attRef.Tag))
_attPos = attPos;
else
_attPos.Add(attRef.Tag, attPos);
draw.Geometry.Draw(attRef);
}
}
}
}
tr.Commit();
}
bref.Rotation = _brefAngle;
draw.Geometry.Draw(bref);
}
return true;
}
}
正在写一个粗糙度标注程序练手,需要改变文字角度,研究下
感谢楼主分享! 学习学习,谢谢楼主分享 我就想看看,还隐藏了什么 看题目好像很简单一个功能,怎么写了这么多代码 学习一下,感谢您分享! 学习了厉害 {:1_12:}{:1_12:}{:1_12:} 回复学习,谢谢分享 感谢楼主分享! 感谢楼主分享! 回复看看{:1_12:} 好东西,你值得拥有!!!! 感谢楼主分享,好例子!!
页:
[1]
2