|
发表于 2015-6-3 14:21:47
|
显示全部楼层
Well, I am not filo327, but I have the exact same question so I'll continue this discussion. Hopefully, since I'm giving some sample code, I will get a more direct response.
This sample overrides the base Line Grips with a single grip on the line's endpoint. I need to be able to allow the user to enter the movement in the X and/or Y directions when DynamicInput is on. Similar to how this ADN c++ example does for a circle. I'm thinking it will need 2 dynamic dimensions (X & Y) that update as the grip is stretched but also allow the user to input a value for either one. Unfortunately, I'm drawing a blank on HOW to achieve this in c#. (Note - Code below started life as an example on the ADN blog, used as a guide by user CivilReminders for use in a commercial product, then stripped down to use for this inquiry.)
[ol]
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
namespace Test_Commands.GripTest
{
public class GripTest
{
private static bool ShouldBeOn = false;
[CommandMethod("GripTestToggle")]
public void griptestCommand()
{
if (ShouldBeOn)
{
ShouldBeOn = false;
RemoveOverrule();
}
else
{
ShouldBeOn = true;
AddOverrule();
}
}
// Added for grips for line
public static TestGripsOverrule mGripOverrule;
public static void AddOverrule()
{
if (ShouldBeOn)
{
// Instantiate our global Overrule and set it to overrule lines with my data attached
mGripOverrule = new TestGripsOverrule();
Overrule.AddOverrule(RXObject.GetClass(typeof(Line)), mGripOverrule, false);
//Turn overruling on
Overrule.Overruling = true;
}
}
public static void RemoveOverrule()
{
if (!ShouldBeOn)
{
Overrule.RemoveOverrule(RXObject.GetClass(typeof(Line)), mGripOverrule);
mGripOverrule = null;
}
}
}
//Grip overrule to add our custom grips to the profile view
public class TestGripsOverrule : GripOverrule
{
public static short gripcolor { get; set; }
//Our custom grip class
//(Could have derived one class for each grip, but we'll use member data (Ordinal property) to distinguish grips instead)
public class MyGrip : GripData
{
private int mGripNum;
public int Ordinal
{
get { return mGripNum; }
set { mGripNum = value; }
}
public override bool ViewportDraw(ViewportDraw worldDraw, ObjectId entityId, DrawType type, Point3d? imageGripPoint, int gripSizeInPixels)
{
Point2d unit = worldDraw.Viewport.GetNumPixelsInUnitSquare(GripPoint);
var gripPolygonPts = new Point3dCollection();
double lengthVal = 1.20 * gripSizeInPixels / unit.X;
gripPolygonPts.Add(new Point3d(GripPoint.X, GripPoint.Y + lengthVal, 0));
gripPolygonPts.Add(new Point3d(GripPoint.X + lengthVal, GripPoint.Y, 0));
gripPolygonPts.Add(new Point3d(GripPoint.X, GripPoint.Y - lengthVal, 0));
gripPolygonPts.Add(new Point3d(GripPoint.X - lengthVal, GripPoint.Y, 0));
// Polygon properties
worldDraw.SubEntityTraits.Color = gripcolor;
worldDraw.SubEntityTraits.FillType = FillType.FillAlways;
worldDraw.Geometry.Polygon(gripPolygonPts);
return true;
}
}
//Array to hold our grip
public GripData[] mGripData = new GripData[1];
public override void GetGripPoints(Autodesk.AutoCAD.DatabaseServices.Entity entity, Autodesk.AutoCAD.DatabaseServices.GripDataCollection grips, double curViewUnitSize, int gripSize, Autodesk.AutoCAD.Geometry.Vector3d curViewDir, Autodesk.AutoCAD.DatabaseServices.GetGripPointsFlags bitFlags)
{
//We assume entity is a profile view
Line line = entity as Line;
if (line == null)
return;
gripcolor = (short)Application.GetSystemVariable("GRIPHOT");
// Set point at end of line
MyGrip grip = new MyGrip();
grip.Ordinal = 0;
mGripData[0] = grip;
UpdateGripLocations(line);
//Add our grips to the list
foreach (MyGrip g in mGripData)
{
grips.Add(g);
}
//Get the standard line grip points as well, but not wanted for this test
//base.GetGripPoints(entity, grips, curViewUnitSize, gripSize, curViewDir, bitFlags);
}
public override void MoveGripPointsAt(Autodesk.AutoCAD.DatabaseServices.Entity entity, GripDataCollection grips,
Vector3d offset, MoveGripPointsFlags bitFlags)
{
//We only take action when we get this call on a database resident entity
//Dragging operation makes shallow clone of line, and setting clomeMeForDragging to false is generally a bad idea.
//(If you do set clone me for dragging to false, then don't call base class overriden methods).
try
{
if (entity.Id.IsValid)
{
var line = entity as Line;
//Iterate through list of all grips being moved
foreach (GripData g in grips)
{
if (g is MyGrip)
{
MyGrip grip = g as MyGrip;
//Cast to our grip type
double newX = g.GripPoint.X + offset.X;
double newY = g.GripPoint.Y + offset.Y;
switch (grip.Ordinal)
{
case 0:
line.EndPoint = new Point3d(newX, newY, line.EndPoint.Z);
break;
default:
break;
}
//Tell grip to move itself long the line
UpdateGripLocations(line);
}
}
}
}
catch (System.Exception)
{
}
}
private void UpdateGripLocations(Line line)
{
Point3d pt = new Point3d(line.EndPoint.X, line.EndPoint.Y, 0);
mGripData[0].GripPoint = pt;
}
}
}
[/ol] |
|