[用户交互] 实现对曲线1/4和3/4长的捕捉
using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.Geometry;using Autodesk.AutoCAD.GraphicsInterface;
ExtensionApplication(
typeof(OsnapApp.CustomOSnapApp))
namespace OsnapApp{
// 注册和卸载自定义捕捉Register and unregister custom osnap
public
class
CustomOSnapApp : IExtensionApplication{
private
QuarterOsnapInfo _info =
new
QuarterOsnapInfo();
private
QuarterGlyph _glyph =
new
QuarterGlyph();
private
CustomObjectSnapMode _mode;
public
void Initialize(){
// 初始化时注册自定义捕捉Register custom osnap on initialize
_mode =
new
CustomObjectSnapMode(
"Quarter",
"Quarter",
"Quarter of length", _glyph );
// 定义使用自定义捕捉的实体类型Which kind of entity will use the osnap
_mode.ApplyToEntityType(
RXObject.GetClass(typeof(Polyline)),
new
AddObjectSnapInfo(_info.SnapInfoPolyline) ); _mode.ApplyToEntityType(
RXObject.GetClass(typeof(Curve)),
new
AddObjectSnapInfo(_info.SnapInfoCurve) ); _mode.ApplyToEntityType(
RXObject.GetClass(typeof(Entity)),
new
AddObjectSnapInfo(_info.SnapInfoEntity) );
// 激活自定义捕捉Activate the osnap
CustomObjectSnapMode.Activate("_Quarter");}
// 卸载自定义捕捉Unregister custom osnap on terminate
public
void Terminate(){
CustomObjectSnapMode.Deactivate("_Quarter");}}
//创建新的1/4对象捕捉 Create new quarter object snap
public
class
QuarterGlyph : Glyph{
private
Point3d _pt;
public
override
void SetLocation(Point3d point) { _pt = point; }
public
override
void ViewportDraw(ViewportDraw vd) {
int glyphPixels =
CustomObjectSnapMode.GlyphSize;
Point2d glyphSize = vd.Viewport.GetNumPixelsInUnitSquare(_pt);
// Calculate the size of the glyph in WCS
// (use for text height factor)
// We'll add 20% to the size, as otherwise
// it looks a little too small
double glyphHeight = (glyphPixels / glyphSize.Y) * 1.2;
string text = "¼";
// Translate the X-axis of the DCS to WCS
// (for the text direction) and the snap
// point itself (for the text location)
Matrix3d e2w = vd.Viewport.EyeToWorldTransform;
Vector3d dir = Vector3d.XAxis.TransformBy(e2w);
Point3d pt = _pt.TransformBy(e2w);
// Draw the centered text representing the glyph
vd.Geometry.Text( pt, vd.Viewport.ViewDirection, dir, glyphHeight, 1, 0, text ); }}
// OSnap info
public
class
QuarterOsnapInfo{
public
void SnapInfoEntity(
ObjectSnapContext context,
ObjectSnapInfo result){
// Nothing here}
public
void SnapInfoCurve(
ObjectSnapContext context,
ObjectSnapInfo result){
// For any curve
Curve cv = context.PickedObject as
Curve;
if (cv == null)
return;
double startParam = cv.StartParam;
double endParam = cv.EndParam;
// Add osnap at first quarter
double param = startParam + ((endParam - startParam) * 0.25);
Point3d pt = cv.GetPointAtParameter(param);
result.SnapPoints.Add(pt);
// Add osnap at third quarter
param = startParam + ((endParam - startParam) * 0.75); pt = cv.GetPointAtParameter(param);
result.SnapPoints.Add(pt);
if (cv.Closed) { pt = cv.StartPoint; result.SnapPoints.Add(pt); }}
public
void SnapInfoPolyline(
ObjectSnapContext context,
ObjectSnapInfo result){
// For polylines
Polyline pl = context.PickedObject as
Polyline;
if (pl == null)
return;
// Get the overall start and end parameters
double plStartParam = pl.StartParam;
double plEndParam = pl.EndParam;
// Get the local
double startParam = plStartParam;
double endParam = startParam + 1.0;
while (endParam <= plEndParam) {
// Calculate the snap point per vertex...
// Add osnap at first quarter
double param = startParam + ((endParam - startParam) * 0.25);
Point3d pt = pl.GetPointAtParameter(param);
result.SnapPoints.Add(pt);
// Add osnap at third quarter
param = startParam + ((endParam - startParam) * 0.75); pt = pl.GetPointAtParameter(param);
result.SnapPoints.Add(pt);
startParam = endParam; endParam += 1.0; }}}}
页:
[1]