Message 1 of 2
Jig with dimension
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
In Mr. @Norman_Yuan blog there is a post which demonstrates how to jig an object with dimension.
I tried to modify the code so that I would pick the first guideline, enter a distance and then the code fixes the jigged entity from the first guideline so it can only be moved in that certain distance. now the code asks for the second guide line, I select it and if I enter distance, it considers both distance values and place the entity accordingly (dist1 from guide line 1, dist2 from guide line 2).
the code works fine with the first distance but does not work ok when the second distance is entered.
I think it has to do some thing with constantly moving basepoint?
public class DimMoveJig : DrawJig
{
private Point3d _basePoint = Point3d.Origin;
private Point3d _movingPoint = Point3d.Origin;
private Line _guideLine1;
private Line _guideLine2;
private Document _dwg;
private Editor _ed;
private Circle _jigEntity= new Circle(Point3d.Origin, Vector3d.ZAxis,5);
private AlignedDimension _dim1 = null;
private AlignedDimension _dim2 = null;
private Point3d _rawMovingPoint;
public DimMoveJig() : base()
{
_dwg = CadApp.DocumentManager.MdiActiveDocument;
_ed = _dwg.Editor;
_dwg.Database.Pdmode = 34;
}
public void Move()
{
if (!SelectGuideLine())
return;
_movingPoint = _basePoint;
_dim1 = new AlignedDimension(_basePoint,_movingPoint,Point3d.Origin,
"0000",_dwg.Database.DimStyleTableId);
using (var tr =_dwg.Database.TransactionManager.StartTransaction())
{
var md = tr.GetObject(_dwg.Database.CurrentSpaceId,OpenMode.ForWrite) as BlockTableRecord;
do
{
var res = _ed.Drag(this);
if (res.Status != PromptStatus.OK)
return;
if (_dim2 == null)
{
md.AppendEntity(_dim1);
tr.AddNewlyCreatedDBObject(_dim1, true);
_dwg.Database.TransactionManager.QueueForGraphicsFlush();
if (SelectGuideLine())
{
_dim2 = new AlignedDimension(_basePoint,_movingPoint,Point3d.Origin,
"0000",_dwg.Database.DimStyleTableId);
}
else
{
tr.Commit();
return;
}
}
else
{
md.AppendEntity(_dim2);
tr.AddNewlyCreatedDBObject(_dim2, true);
md.AppendEntity(_jigEntity);
var dbpt = new DBPoint(_basePoint);
tr.AddNewlyCreatedDBObject(_jigEntity, true);
md.AppendEntity(dbpt);
tr.AddNewlyCreatedDBObject(dbpt, true);
tr.Commit();
return;
}
} while (true);
}
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
var status = SamplerStatus.NoChange;
var opt = new JigPromptPointOptions("\nMove to:");
opt.UseBasePoint = true;
var guideLine1Vector = new Vector3d();
if(_dim2 is null)
{
_basePoint = _guideLine1.GetClosestPointTo(_movingPoint, true);
_dim1.XLine1Point = _basePoint;
}
else
{
guideLine1Vector = _dim1.XLine1Point.GetVectorTo(_dim1.XLine2Point);
var closestPointOnGuideLine1 = _guideLine1.GetClosestPointTo(_movingPoint, true);
_dim2.XLine2Point = closestPointOnGuideLine1.Add(guideLine1Vector);
_dim2.XLine1Point = _guideLine2.GetClosestPointTo(_dim2.XLine2Point, true);
_basePoint = _dim2.XLine1Point;
}
opt.BasePoint = _basePoint;
var res = prompts.AcquirePoint(opt);
if (res.Status == PromptStatus.OK)
{
if (!res.Value.Equals(_rawMovingPoint))
{
_rawMovingPoint = res.Value;
if (_dim2 is null)
{
_dim1.XLine2Point = res.Value;
_dim1.DimLinePoint = res.Value;
var dist = _basePoint.DistanceTo(res.Value).ToString("######0.000");
_dim1.DimensionText = dist;
_jigEntity.Center = _dim1.XLine2Point;
_movingPoint = res.Value;
}
else
{
var closestPointOnGuideLine1 = _guideLine1.GetClosestPointTo(res.Value, true);
_dim2.XLine2Point = closestPointOnGuideLine1.Add(guideLine1Vector);
_dim2.XLine1Point = _guideLine2.GetClosestPointTo(_dim2.XLine1Point, true);
var dist = _dim2.XLine1Point.DistanceTo(_dim2.XLine2Point).ToString("######0.000");
_dim2.DimensionText = dist;
_dim2.DimLinePoint = res.Value;
_jigEntity.Center = _dim2.XLine2Point;
_movingPoint = _dim2.XLine2Point;
}
status = SamplerStatus.OK;
}
}
else
{
status = SamplerStatus.Cancel;
}
return status;
}
protected override bool WorldDraw(WorldDraw draw)
{
if (_dim1.ObjectId == ObjectId.Null)
{
_dim1.RecomputeDimensionBlock(true);
draw.Geometry.Draw(_dim1);
}
if(_dim2 != null)
{
_dim2.RecomputeDimensionBlock(true);
draw.Geometry.Draw(_dim2);
}
return draw.Geometry.Draw(_jigEntity);
}
private bool SelectGuideLine()
{
var eRes = _ed.GetEntity("\nSelect guide line");
if (eRes.Status == PromptStatus.OK)
{
using (var tr =_dwg.Database.TransactionManager.StartOpenCloseTransaction())
{
if (_dim1 is null)
{
_guideLine1 = tr.GetObject(eRes.ObjectId, OpenMode.ForRead) as Line;
if (_guideLine1 is null)
return false;
_basePoint = eRes.PickedPoint;
}
else
{
_guideLine2 = tr.GetObject(eRes.ObjectId, OpenMode.ForRead) as Line;
if (_guideLine2 is null)
return false;
_basePoint = _guideLine2.GetClosestPointTo(_dim1.XLine2Point, true);
var l = new Line(_basePoint, _dim1.XLine2Point);
var md = tr.GetObject(CadApp.DocumentManager.MdiActiveDocument.Database.CurrentSpaceId,
OpenMode.ForWrite) as BlockTableRecord;
}
tr.Commit();
}
return true;
}
else
{
return false;
}
}
}