1. 工具类:PromptTool.cs
using Autodesk. AutoCAD. EditorInput ;
using Autodesk. AutoCAD. Geometry ;
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace _04用户交互
{
public static partial class PromptTool
{
public static PromptPointResult GetPoint2 ( this Editor ed, string promptStr)
{
PromptPointOptions ppo = new PromptPointOptions ( promptStr) ;
ppo. AllowNone = true ;
return ed. GetPoint ( ppo) ;
}
public static PromptPointResult GetPoint ( this Editor ed, string promptStr, Point3d pointBase, params string [ ] keyWord)
{
PromptPointOptions ppo = new PromptPointOptions ( promptStr) ;
ppo. AllowNone = true ;
for ( int i = 0 ; i < keyWord. Length; i++ )
{
ppo. Keywords. Add ( keyWord[ i] ) ;
}
ppo. AppendKeywordsToMessage = false ;
ppo. BasePoint = pointBase;
ppo. UseBasePoint = true ;
ppo. UseDashedLine = false ;
return ed. GetPoint ( ppo) ;
}
}
}
2. Demo测试
[ CommandMethod ( "PromptDemo" ) ]
public void PromptDemo ( )
{
Database db = HostApplicationServices. WorkingDatabase;
Editor ed = Application. DocumentManager. MdiActiveDocument. Editor;
Point3d p1 = new Point3d ( 0 , 0 , 0 ) ;
Point3d p2 = new Point3d ( ) ;
PromptPointOptions ppo = new PromptPointOptions ( "请指定第一个点:" ) ;
ppo. AllowNone = true ;
PromptPointResult ppr = GetPoint ( ppo) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. OK) p1 = ppr. Value;
ppo. Message = "请指定第二个点" ;
ppo. BasePoint = p1;
ppo. UseBasePoint = true ;
ppr = GetPoint ( ppo) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) return ;
if ( ppr. Status == PromptStatus. OK) p2 = ppr. Value;
db. AddLineToModelSpace ( p1, p2) ;
}
private PromptPointResult GetPoint ( PromptPointOptions ppo)
{
ppo. AllowNone = true ;
Editor ed = Application. DocumentManager. MdiActiveDocument. Editor;
return ed. GetPoint ( ppo) ;
}
3. 仿系统直线命令
3.1 拖曳类的引入
using Autodesk. AutoCAD. DatabaseServices ;
using Autodesk. AutoCAD. EditorInput ;
using Autodesk. AutoCAD. Geometry ;
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace _04用户交互
{
public class LineJig : EntityJig
{
private Point3d jStartPoint;
private Point3d jEndPoint;
private string jPromtStr;
private string [ ] jKeywords;
public LineJig ( Point3d startPoint, string promtStr, string [ ] keywords)
: base ( new Line ( ) )
{
jStartPoint = startPoint;
( ( Line) Entity) . StartPoint = jStartPoint;
jPromtStr = promtStr;
jKeywords = keywords;
}
protected override bool Update ( )
{
( ( Line) Entity) . EndPoint = jEndPoint;
return true ;
}
protected override SamplerStatus Sampler ( JigPrompts prompts)
{
JigPromptPointOptions jppo = new JigPromptPointOptions ( jPromtStr) ;
for ( int i = 0 ; i < jKeywords. Length; i++ )
{
jppo. Keywords. Add ( jKeywords[ i] ) ;
}
char space = ( char ) 32 ;
jppo. Keywords. Add ( space. ToString ( ) ) ;
jppo. UserInputControls = UserInputControls. Accept3dCoordinates;
jppo. AppendKeywordsToMessage = false ;
PromptPointResult pr = prompts. AcquirePoint ( jppo) ;
jEndPoint = pr. Value;
return SamplerStatus. NoChange;
}
public Entity GetEntity ( )
{
return Entity;
}
}
}
3.2 测试实现
[ CommandMethod ( "FangLine" ) ]
public void FangLine ( )
{
Database db = HostApplicationServices. WorkingDatabase;
Editor ed = Application. DocumentManager. MdiActiveDocument. Editor;
List< ObjectId> lineList = new List< ObjectId> ( ) ;
Point3d pointStart = new Point3d ( 100 , 100 , 0 ) ;
Point3d pointPre = new Point3d ( 100 , 100 , 0 ) ;
PromptPointResult ppr = ed. GetPoint2 ( "\n 指定第一个点:" ) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) pointPre = pointStart;
if ( ppr. Status == PromptStatus. OK)
{
pointStart = ppr. Value;
pointPre = pointStart;
}
bool isC = true ;
while ( isC)
{
if ( lineList. Count > 1 )
{
ppr = ed. GetPoint ( "\n 指定下一点或 [闭合(C)/放弃(U)]:" , pointPre, new string [ ] { "C" , "U" } ) ;
}
else
{
ppr = ed. GetPoint ( "\n 指定下一点或 [放弃(U)]" , pointPre, new string [ ] { "U" } ) ;
}
Point3d pointNext;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) return ;
if ( ppr. Status == PromptStatus. OK)
{
pointNext = ppr. Value;
lineList. Add ( db. AddLineToModelSpace ( pointPre, pointNext) ) ;
pointPre = pointNext;
}
if ( ppr. Status == PromptStatus. Keyword)
{
switch ( ppr. StringResult)
{
case "U" :
if ( lineList. Count == 0 )
{
pointStart = new Point3d ( 100 , 100 , 0 ) ;
pointPre = new Point3d ( 100 , 100 , 0 ) ;
ppr = ed. GetPoint2 ( "\n 指定第一个点:" ) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) pointPre = pointStart;
if ( ppr. Status == PromptStatus. OK)
{
pointStart = ppr. Value;
pointPre = pointStart;
}
}
else if ( lineList. Count > 0 )
{
int count = lineList. Count;
ObjectId lId = lineList. ElementAt ( count- 1 ) ;
pointPre = this . GetLineStartPoint ( lId) ;
lineList. RemoveAt ( count- 1 ) ;
lId. EraseEntity ( ) ;
}
break ;
case "C" :
lineList. Add ( db. AddLineToModelSpace ( pointPre, pointStart) ) ;
isC = false ;
break ;
}
}
}
}
[ CommandMethod ( "FangLine2" ) ]
public void FangLine2 ( )
{
Database db = HostApplicationServices. WorkingDatabase;
Editor ed = Application. DocumentManager. MdiActiveDocument. Editor;
List< ObjectId> lineList = new List< ObjectId> ( ) ;
Point3d pointStart = new Point3d ( 100 , 100 , 0 ) ;
Point3d pointPre = new Point3d ( 100 , 100 , 0 ) ;
PromptPointResult ppr = ed. GetPoint2 ( "\n 指定第一个点:" ) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) pointPre = pointStart;
if ( ppr. Status == PromptStatus. OK)
{
pointStart = ppr. Value;
pointPre = pointStart;
}
LineJig lineJig;
PromptResult pr;
bool isC = true ;
while ( isC)
{
if ( lineList. Count > 1 )
{
lineJig = new LineJig ( pointPre, "\n 指定下一点或 [闭合(C)/放弃(U)]:" , new string [ ] { "C" , "U" } ) ;
pr = ed. Drag ( lineJig) ;
}
else
{
lineJig = new LineJig ( pointPre, "\n 指定下一点或 [放弃(U)]" , new string [ ] { "U" } ) ;
pr = ed. Drag ( lineJig) ;
}
if ( pr. Status == PromptStatus. Cancel) return ;
if ( pr. Status == PromptStatus. None) return ;
if ( pr. Status == PromptStatus. OK)
{
Line line = ( Line) lineJig. GetEntity ( ) ;
pointPre = line. EndPoint;
lineList. Add ( db. AddEnityToModelSpace ( line) ) ;
}
if ( pr. Status == PromptStatus. Keyword)
{
switch ( pr. StringResult)
{
case "U" :
if ( lineList. Count == 0 )
{
pointStart = new Point3d ( 100 , 100 , 0 ) ;
pointPre = new Point3d ( 100 , 100 , 0 ) ;
ppr = ed. GetPoint2 ( "\n 指定第一个点:" ) ;
if ( ppr. Status == PromptStatus. Cancel) return ;
if ( ppr. Status == PromptStatus. None) pointPre = pointStart;
if ( ppr. Status == PromptStatus. OK)
{
pointStart = ppr. Value;
pointPre = pointStart;
}
}
else if ( lineList. Count > 0 )
{
int count = lineList. Count;
ObjectId lId = lineList. ElementAt ( count - 1 ) ;
pointPre = this . GetLineStartPoint ( lId) ;
lineList. RemoveAt ( count - 1 ) ;
lId. EraseEntity ( ) ;
}
break ;
case "C" :
lineList. Add ( db. AddLineToModelSpace ( pointPre, pointStart) ) ;
isC = false ;
break ;
case " " :
isC = false ;
break ;
}
}
}
}
private Point3d GetLineStartPoint ( ObjectId lineId)
{
Point3d startPoint;
Database db = HostApplicationServices. WorkingDatabase;
using ( Transaction trans = db. TransactionManager. StartTransaction ( ) )
{
Line line = ( Line) lineId. GetObject ( OpenMode. ForRead) ;
startPoint = line. StartPoint;
}
return startPoint;
}
4. 仿系统圆命令
4.1 拖曳类的引入
using Autodesk. AutoCAD. DatabaseServices ;
using Autodesk. AutoCAD. EditorInput ;
using Autodesk. AutoCAD. Geometry ;
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using AcDotNetTool ;
namespace _04用户交互
{
public class CircleJig : EntityJig
{
private double jRadius;
public CircleJig ( Point3d center)
: base ( new Circle ( ) )
{
( ( Circle) Entity) . Center = center;
}
protected override bool Update ( )
{
if ( jRadius > 0 )
{
( ( Circle) Entity) . Radius = jRadius;
}
return true ;
}
protected override SamplerStatus Sampler ( JigPrompts prompts)
{
JigPromptPointOptions jppo = new JigPromptPointOptions ( "\n 请指定园上的一个点" ) ;
char space = ( char ) 32 ;
jppo. Keywords. Add ( "U" ) ;
jppo. Keywords. Add ( space. ToString ( ) ) ;
jppo. UserInputControls = UserInputControls. Accept3dCoordinates;
jppo. Cursor = CursorType. RubberBand;
jppo. BasePoint = ( ( Circle) Entity) . Center;
jppo. UseBasePoint = true ;
PromptPointResult ppr = prompts. AcquirePoint ( jppo) ;
jRadius = ppr. Value. GetDistanceBetweenTwoPoint ( ( ( Circle) Entity) . Center) ;
return SamplerStatus. NoChange;
}
public Entity GetEntity ( )
{
return Entity;
}
}
}
4.2 测试实现
[ CommandMethod ( "CircleDemo" ) ]
public void CircleDemo ( )
{
Database db = HostApplicationServices. WorkingDatabase;
Editor ed = Application. DocumentManager. MdiActiveDocument. Editor;
Point3d center = new Point3d ( ) ;
PromptPointResult ppr = ed. GetPoint ( "\n 请制定圆心:" ) ;
if ( ppr. Status == PromptStatus. OK)
{
center = ppr. Value;
}
#region
#endregion
CircleJig jCircle = new CircleJig ( center) ;
PromptResult pr = ed. Drag ( jCircle) ;
if ( pr. Status == PromptStatus. OK)
{
db. AddEnityToModelSpace ( jCircle. GetEntity ( ) ) ;
}
}