71
Selection sets / Re: Selection Ordinate Dimensions with Filter (dxf code)
« Last post by madmat on February 03, 2013, 12:13:07 PM »Thanx Gile for your answers, I've tried your last solution and it's working fine.
[CommandMethod("Test")]
public void test()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] filter = { new TypedValue(0, "dimension"), new TypedValue(3, "ISO-25") };
ed.SelectionAdded += ed_SelectionAdded;
PromptSelectionResult psr = ed.GetSelection(new SelectionFilter(filter));
ed.SelectionAdded -= ed_SelectionAdded;
if (psr.Status != PromptStatus.OK) return;
ed.SetImpliedSelection(psr.Value);
}
void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
{
for (int i = 0; i < e.AddedObjects.Count; i++)
{
if (e.AddedObjects[i].ObjectId.ObjectClass.Name != "AcDbOrdinateDimension")
e.Remove(i);
}
} <CommandMethod("Test")> _
Public Sub test()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim filter As TypedValue() = { New TypedValue(0, "dimension"), new TypedValue(3, "ISO-25") }
AddHandler ed.SelectionAdded, AddressOf ed_SelectionAdded
Dim psr As PromptSelectionResult = ed.GetSelection(New SelectionFilter(filter))
RemoveHandler ed.SelectionAdded, AddressOf ed_SelectionAdded
If psr.Status <> PromptStatus.OK Then
Return
End If
ed.SetImpliedSelection(psr.Value)
End Sub
Private Sub ed_SelectionAdded(sender As Object, e As SelectionAddedEventArgs)
For i As Integer = 0 To e.AddedObjects.Count - 1
If e.AddedObjects(i).ObjectId.ObjectClass.Name <> "AcDbOrdinateDimension" Then
e.Remove(i)
End If
Next
End SubTypedValue[] tvs = {
new TypedValue(0, "dimension"),
new TypedValue(3, "ISO-25"),
new TypedValue(-4, "&="),
new TypedValue(70, 6)};
SelectionFilter filter = new SelectioFilter(tvs);
PromptSelectionResult psr = ed.GetSelection(filter); class BlockJig : EntityJig
{
protected BlockReference _br;
protected Point3d _pos;
protected double _rot, _ucsRot;
public BlockJig(BlockReference br)
: base(br)
{
_br = br;
_pos = _br.Position;
Editor ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
CoordinateSystem3d ucs = ed.CurrentUserCoordinateSystem.CoordinateSystem3d;
Matrix3d ocsMat = Matrix3d.WorldToPlane(new Plane(Point3d.Origin, ucs.Zaxis));
_ucsRot = Vector3d.XAxis.GetAngleTo(ucs.Xaxis.TransformBy(ocsMat), ucs.Zaxis);
_rot = _br.Rotation - _ucsRot;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
System.Windows.Forms.Keys mods = System.Windows.Forms.Control.ModifierKeys;
if ((mods & System.Windows.Forms.Keys.Control) > 0)
{
JigPromptAngleOptions jpao =
new JigPromptAngleOptions("\nSpecify the rotation: ");
jpao.UseBasePoint = true;
jpao.BasePoint = _br.Position;
jpao.Cursor = CursorType.RubberBand;
jpao.UserInputControls = (
UserInputControls.Accept3dCoordinates |
UserInputControls.UseBasePointElevation);
PromptDoubleResult pdr = prompts.AcquireAngle(jpao);
if (_rot == pdr.Value)
{
return SamplerStatus.NoChange;
}
else
{
_rot = pdr.Value;
return SamplerStatus.OK;
}
}
else
{
JigPromptPointOptions jppo =
new JigPromptPointOptions("\nSpecify insertion point (or press Ctrl for rotation): ");
jppo.UserInputControls =
(UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
PromptPointResult ppr = prompts.AcquirePoint(jppo);
if (_pos.DistanceTo(ppr.Value) < Tolerance.Global.EqualPoint)
{
return SamplerStatus.NoChange;
}
else
{
_pos = ppr.Value;
}
return SamplerStatus.OK;
}
}
protected override bool Update()
{
_br.Position = _pos;
_br.Rotation = _rot +_ucsRot;
return true;
}
}
class BlockAttribJig : BlockJig
{
struct TextInfo
{
public Point3d Position;
public Point3d Alignment;
public double Rotation;
public bool IsAligned;
}
private Dictionary<string, TextInfo> _attInfos;
public BlockAttribJig(BlockReference br)
: base(br)
{
_attInfos = new Dictionary<string, TextInfo>();
BlockTableRecord btr = (BlockTableRecord)br.BlockTableRecord.GetObject(OpenMode.ForRead);
foreach (ObjectId id in btr)
{
if (id.ObjectClass.Name == "AcDbAttributeDefinition")
{
AttributeDefinition attDef = (AttributeDefinition)id.GetObject(OpenMode.ForRead);
TextInfo ti = new TextInfo()
{
Position = attDef.Position,
Alignment = attDef.AlignmentPoint,
IsAligned = attDef.Justify != AttachmentPoint.BaseLeft,
Rotation = attDef.Rotation
};
_attInfos.Add(attDef.Tag.ToUpper(), ti);
}
}
}
protected override bool Update()
{
base.Update();
foreach (ObjectId id in _br.AttributeCollection)
{
AttributeReference att = (AttributeReference)id.GetObject(OpenMode.ForWrite);
att.Rotation = _br.Rotation;
string tag = att.Tag.ToUpper();
if (_attInfos.ContainsKey(tag))
{
TextInfo ti = _attInfos[tag];
att.Position = ti.Position.TransformBy(_br.BlockTransform);
if (ti.IsAligned)
{
att.AlignmentPoint =
ti.Alignment.TransformBy(_br.BlockTransform);
att.AdjustAlignment(_br.Database);
}
if (att.IsMTextAttribute)
{
att.UpdateMTextAttribute();
}
att.Rotation = ti.Rotation + _br.Rotation;
}
}
return true;
}
} [CommandMethod("TEST")]
public void Test()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptResult pr = ed.GetString("\nBlock name: ");
if (pr.Status != PromptStatus.OK) return;
string blockName = pr.StringResult;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blockName))
{
ed.WriteMessage("\nNone block '{0}' in the document block table.", blockName);
return;
}
BlockTableRecord curSpace =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// Add the block reference to Database first
BlockReference br = new BlockReference(Point3d.Origin, bt[blockName]);
br.TransformBy(ed.CurrentUserCoordinateSystem);
curSpace.AppendEntity(br);
tr.AddNewlyCreatedDBObject(br, true);
// Get the block definition
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(bt[blockName], OpenMode.ForRead);
BlockJig jig;
if (btr.HasAttributeDefinitions)
{
// Add attribute references to the block reference
foreach (ObjectId id in btr)
{
if (id.ObjectClass.Name == "AcDbAttributeDefinition")
{
AttributeDefinition attDef =
(AttributeDefinition)tr.GetObject(id, OpenMode.ForRead);
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
ObjectId attId = br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
// Create a BlockAttribJig instance
jig = new BlockAttribJig(br);
}
else
{
// Create a BlockJig instance
jig = new BlockJig(br);
}
// Drag the block reference
pr = ed.Drag(jig);
if (pr.Status != PromptStatus.OK) br.Erase();
tr.Commit();
}
} [CommandMethod("PlaceMtextToMidpoint","mit", CommandFlags.Modal | CommandFlags.UsePickSet | CommandFlags.Redraw)]
public void PlaceMtextToMiddle()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("\nSelect the first curve: ");
peo.SetRejectMessage("Only a curve !");
peo.AddAllowedClass(typeof(Curve), false);
PromptEntityResult per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId id1 = per.ObjectId;
Point3d p1 = per.PickedPoint;
peo.Message = "\nSelect the second curve: ";
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK) return;
ObjectId id2 = per.ObjectId;
PromptStringOptions pso = new PromptStringOptions("\nEnter first line of Mtext: ");
pso.AllowSpaces = true;
PromptResult res;
res = ed.GetString(pso);
if (res.Status != PromptStatus.OK)
return;
string txtStr = res.StringResult;
do
{
pso.Message = "\nEnter next line of Mtext: ";
res = ed.GetString(pso);
if (res.Status != PromptStatus.OK)
return;
txtStr = txtStr + "\\P" + res.StringResult;
} while (res.Status != PromptStatus.OK);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBObject obj1 = (DBObject)tr.GetObject(id1, OpenMode.ForRead, false);
Curve curve1 = obj1 as Curve;
if (curve1 == null)
{
ed.WriteMessage("\nNo curve selected\n");
return;
}
DBObject obj2 = (DBObject)tr.GetObject(id2, OpenMode.ForRead, false);
Curve curve2 = obj2 as Curve;
if (curve2 == null)
{
ed.WriteMessage("\nNo curve selected\n");
return;
}
Point3d p2 = curve2.GetClosestPointTo(p1, false);
Plane plan = curve1.GetPlane();
Vector3d norm = plan.Normal;
p1 = p1.TransformBy(Matrix3d.WorldToPlane(norm));
p2 = p2.TransformBy(Matrix3d.WorldToPlane(norm));
// calculate middle point one way
Point3d mp = p1.Add(p2 - p1).ScaleBy(0.5, p1).TransformBy(Matrix3d.WorldToPlane(norm));
double ang = (p2 - p1).AngleOnPlane(plan);
// draw vectors for imagination
ed.DrawVector(p1, mp, 1, true);
ed.DrawVector(p2, mp, 5, true);
// set angle to be more readable
if ((ang > Math.PI / 2) && (ang < Math.PI * 1.5))
ang = ang + Math.PI;
// create mtext
MText mtxt = new MText();
mtxt.SetDatabaseDefaults();
mtxt.Attachment = AttachmentPoint.MiddleCenter;
mtxt.Location = mp;
mtxt.TextHeight = db.Textsize;
mtxt.Width = 0.0;
mtxt.Contents = txtStr;
mtxt.Layer = "0";
mtxt.ColorIndex = 256;
Matrix3d mx = Matrix3d.Rotation(ang, norm, mp);
mtxt.TransformBy(mx);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
// add mtext to database and transaction
btr.AppendEntity(mtxt);
tr.AddNewlyCreatedDBObject(mtxt, true);
tr.Commit();
}
} if (b.Name == "WT")
{
// HELP!!!
// Here i need to change the properties of the block with name "WT"
foreach (object item in b)
{
Autodesk.AutoCAD.Interop.Common.AcadLine ln = item as Autodesk.AutoCAD.Interop.Common.AcadLine;
if (ln != null)
{
ln.Linetype = "Center";
ln.LinetypeScale = 0.01;
Autodesk.AutoCAD.Interop.Common.AcadAcCmColor acCol = ln.TrueColor;
acCol.ColorIndex = Autodesk.AutoCAD.Interop.Common.AcColor.acCyan;
ln.TrueColor = acCol;
}
}
}