To Inherit DrawJig you have to override 2 Method : Sampler and WorldDraw
Sampler simply tell the Jig to sample the needed data, and check if anything needs a redraw
WorldDraw you can then update the enities so that the changes detected in the sampler method are shown on screen
Here in below code in Sample method we are looking for the current mouse position, and we will remember the difference of the current postition with the new postion. we are also reterning samper states matching the outcome of the current prompt.
In worldDraw function, we can now edit the polylines with the calculated vector and draw these changes to the screen.
publicstaticclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){try{using(Transactiontransaction=ActiveUtil.TransactionManager.StartTransaction()){PromptSelectionOptionspromptSelection=newPromptSelectionOptions();PromptSelectionResultresult=ActiveUtil.Editor.GetSelection(promptSelection);if(result.Status!=PromptStatus.OK){return;}// Iterate results to find polylines (I know filters are smarter..)List<Polyline>polylines=newList<Polyline>();foreach(ObjectIdobjectIdinresult.Value.GetObjectIds()){if(objectId.IsValidObjectType(typeof(Polyline))){varpolyline=objectId.GetObject<Polyline>(OpenMode.ForWrite);polylines.Add(polyline);}}// prompt refernce pointPromptPointOptionspromptPoint=newPromptPointOptions("select reference point");PromptPointResultpromptPointResult=ActiveUtil.Editor.GetPoint(promptPoint);if(promptPointResult.Status!=PromptStatus.OK){return;}// Jig It.SimpleGeometryJigjig=newSimpleGeometryJig(polylines,promptPointResult.Value);PromptResultres=ActiveUtil.Editor.Drag(jig);transaction.Commit();}}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}}publicclassSimpleGeometryJig:DrawJig{privateList<Polyline>_polylines;privatePoint3d_currentPosition;privateVector2d_currentVector;publicSimpleGeometryJig(List<Polyline>polylines,Point3dreferencePoint){_polylines=polylines;_currentPosition=referencePoint;_currentVector=newVector2d(0,0);}protectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsjigOpt=newJigPromptPointOptions("select insertion point");jigOpt.UserInputControls=UserInputControls.Accept3dCoordinates;PromptPointResultres=prompts.AcquirePoint(jigOpt);if(res.Status!=PromptStatus.OK){returnSamplerStatus.Cancel;}if(res.Value.IsEqualTo(_currentPosition,newTolerance(0.1,0.1))){returnSamplerStatus.NoChange;}Vector3dv3d=_currentPosition.GetVectorTo(res.Value);_currentVector=newVector2d(v3d.X,v3d.Y);_currentPosition=res.Value;returnSamplerStatus.OK;}protectedoverrideboolWorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDrawdraw){try{foreach(varplin_polylines){for(inti=0;i<pl.NumberOfVertices;i++){pl.SetPointAt(i,pl.GetPoint2dAt(i).Add(_currentVector));}draw.Geometry.Draw(pl);}}catch(System.Exception){returnfalse;}returntrue;}}
Code to Insert Two Circles with DrawJig
publicstaticclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){try{using(Transactiontransaction=ActiveUtil.TransactionManager.StartTransaction()){//Code to draw two circles based on base pointvarbasePoint=newGeometry.Entities.Point2D();varcircle1=newCadCircle(basePoint,10);circle1.Append();varcircle2=newCadCircle(basePoint,20);circle2.Append();varcircles=newList<CadCircle>{circle1,circle2};// Jig It.SimpleGeometryJigjig=newSimpleGeometryJig(circles,basePoint);PromptResultres=ActiveUtil.Editor.Drag(jig);//code to adjust it's positiontransaction.Commit();}}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}}publicclassSimpleGeometryJig:DrawJig{privateList<CadCircle>_circles;privatePoint2D_basePoint;privatePoint3d_currentPosition;publicSimpleGeometryJig(List<CadCircle>circles,Point2DbasePoint){_circles=circles;_currentPosition=newPoint3d(basePoint.X,basePoint.Y,0);}protectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsjigOpt=newJigPromptPointOptions("select insertion point");jigOpt.UserInputControls=UserInputControls.Accept3dCoordinates;PromptPointResultres=prompts.AcquirePoint(jigOpt);if(res.Status!=PromptStatus.OK){returnSamplerStatus.Cancel;}if(res.Value.IsEqualTo(_currentPosition,newTolerance(0.1,0.1))){returnSamplerStatus.NoChange;}_currentPosition=res.Value;returnSamplerStatus.OK;}protectedoverrideboolWorldDraw(WorldDrawdraw){try{foreach(varcirclein_circles){circle.Circle.Center=_currentPosition;draw.Geometry.Draw(circle.Entity);}returntrue;}catch(System.Exception){returnfalse;}}}
Code to Create Rectangle using Two Corner Point with DrawJig
publicclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){PromptPointResultpr=ActiveUtil.Editor.GetPoint("\nSelect First Corner of Rectangle:");if(pr.Status!=PromptStatus.OK){return;}varjigger=newRectangleDrawJig(pr.Value);ActiveUtil.Editor.Drag(jigger);using(Transactiontransaction=ActiveUtil.TransactionManager.StartTransaction()){Polylineent=newPolyline();ent.SetDatabaseDefaults();varcornerPoints=jigger.GetCornerPoints();varcounter=newCounter(0);foreach(varpointincornerPoints){ent.AddVertexAt(counter.CreateId(),newPoint2d(point.X,point.Y),0,0,0);}ent.Closed=true;ActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(ent);transaction.AddNewlyCreatedDBObject(ent,true);transaction.Commit();}}}publicclassRectangleDrawJig:DrawJig{privatePoint3d_CornerPoint1;privatePoint3d_CornerPoint2;publicRectangleDrawJig(Point3dbasePt){_CornerPoint1=basePt;}publicList<Point3d>GetCornerPoints(){varpoints=newList<Point3d>();points.Add(_CornerPoint1);points.Add(newPoint3d(_CornerPoint1.X,_CornerPoint2.Y,0));points.Add(_CornerPoint2);points.Add(newPoint3d(_CornerPoint2.X,_CornerPoint1.Y,0));returnpoints;}protectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsprOptions2=newJigPromptPointOptions("\nCorner2:");prOptions2.UseBasePoint=false;PromptPointResultprResult2=prompts.AcquirePoint(prOptions2);if(prResult2.Status==PromptStatus.Cancel||prResult2.Status==PromptStatus.Error){returnSamplerStatus.Cancel;}Point3dtmpPt=prResult2.Value.TransformBy(ActiveUtil.Editor.CurrentUserCoordinateSystem.Inverse());if(!_CornerPoint2.IsEqualTo(tmpPt,newTolerance(10e-10,10e-10))){_CornerPoint2=tmpPt;returnSamplerStatus.OK;}else{returnSamplerStatus.NoChange;}}protectedoverrideboolWorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDrawdraw){draw.Geometry?.Polygon(newPoint3dCollection(GetCornerPoints().ToArray()));returntrue;}}
Sample code to draw rectangle with two corner point or one corner point + rotation angle
usingAutodesk.AutoCAD.DatabaseServices;usingAutodesk.AutoCAD.EditorInput;usingAutodesk.AutoCAD.Geometry;usingAutodesk.AutoCAD.Runtime;usingstaticSystem.Math;usingAcAp=Autodesk.AutoCAD.ApplicationServices.Core.Application;usingAcGi=Autodesk.AutoCAD.GraphicsInterface;namespaceCADHelperTest{publicclassCommands{[CommandMethod("TEST")]publicvoidTest(){vardoc=AcAp.DocumentManager.MdiActiveDocument;vardb=doc.Database;vared=doc.Editor;varppr=ed.GetPoint("\nSpecify first corner point: ");if(ppr.Status!=PromptStatus.OK)return;varpt=ppr.Value;using(vartr=db.TransactionManager.StartTransaction())using(varpline=newPolyline(4)){pline.AddVertexAt(0,newPoint2d(pt.X,pt.Y),0.0,0.0,0.0);pline.AddVertexAt(1,newPoint2d(pt.X+1.0,pt.Y),0.0,0.0,0.0);pline.AddVertexAt(2,newPoint2d(pt.X+1.0,pt.Y+1.0),0.0,0.0,0.0);pline.AddVertexAt(3,newPoint2d(pt.X,pt.Y+1.0),0.0,0.0,0.0);pline.Closed=true;pline.Elevation=pt.Z;varucs=ed.CurrentUserCoordinateSystem;pline.TransformBy(ucs);// create a RectangleJigvarrectJig=newRectangleJig(pline,0.0);// Loop while the user specify other corner or cancelwhile(true){varpr=ed.Drag(rectJig);// Other corner is specifiedif(pr.Status==PromptStatus.OK){varcurSpace=(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);curSpace.AppendEntity(pline);tr.AddNewlyCreatedDBObject(pline,true);break;}// Rotation optionif(pr.Status==PromptStatus.Keyword){// use a RotationJig to get the rectangle rotationvarrotJig=newRotationJig(pline,pline.GetPoint3dAt(0),pline.Normal);varresult=ed.Drag(rotJig);if(result.Status==PromptStatus.OK)rectJig=newRectangleJig(pline,rotJig.Rotation);elsebreak;}// Cancelelsebreak;}tr.Commit();}}}publicstructRectangle{publicPoint2dPoint0{get;}publicPoint2dPoint1{get;}publicPoint2dPoint2{get;}publicPoint2dPoint3{get;}publicRectangle(Point2dfirstCorner,Point2doppositeCorner,doublerotation){Vector2du=newVector2d(Cos(rotation),Sin(rotation));Vector2dv=newVector2d(-Sin(rotation),Cos(rotation));Vector2ddiag=firstCorner.GetVectorTo(oppositeCorner);Point0=firstCorner;Point1=firstCorner+u*u.DotProduct(diag);Point2=oppositeCorner;Point3=firstCorner+v*v.DotProduct(diag);}}publicclassRectangleJig:EntityJig{privatePolylinepline;doubleucsRot,rotation;Planeplane;Point3ddragPt,basePt;privatePoint2dpt2D;Editored;CoordinateSystem3ducs;publicRectangleJig(Polylinepline,doublerotation):base(pline){this.pline=pline;this.rotation=rotation;plane=newPlane(Point3d.Origin,pline.Normal);basePt=pline.GetPoint3dAt(0);pt2D=pline.GetPoint2dAt(0);ed=AcAp.DocumentManager.MdiActiveDocument.Editor;ucs=ed.CurrentUserCoordinateSystem.CoordinateSystem3d;Matrix3dmat=Matrix3d.WorldToPlane(plane);ucsRot=Vector3d.XAxis.GetAngleTo(ucs.Xaxis.TransformBy(mat),Vector3d.ZAxis);}protectedoverrideSamplerStatusSampler(JigPromptsprompts){varmsg="\nSpecify other corner point or [Rotation]: ";varoptions=newJigPromptPointOptions(msg,"Rotation");options.AppendKeywordsToMessage=true;options.UseBasePoint=true;options.BasePoint=basePt;options.UserInputControls=UserInputControls.Accept3dCoordinates|UserInputControls.UseBasePointElevation;PromptPointResultresult=prompts.AcquirePoint(options);if(result.Status==PromptStatus.Keyword){pline.TransformBy(Matrix3d.Rotation(-rotation,pline.Normal,basePt));returnSamplerStatus.OK;}if(result.Value.IsEqualTo(dragPt))returnSamplerStatus.NoChange;dragPt=result.Value;returnSamplerStatus.OK;}protectedoverrideboolUpdate(){varrectangle=newRectangle(pt2D,dragPt.Convert2d(plane),rotation+ucsRot);pline.SetPointAt(1,rectangle.Point1);pline.SetPointAt(2,rectangle.Point2);pline.SetPointAt(3,rectangle.Point3);returntrue;}}publicclassRotationJig:DrawJig{privateEntityentity;doublerotation;Point3dbasePoint;Vector3dnormal;publicdoubleRotation=>rotation;publicRotationJig(Entityentity,Point3dbasePoint,Vector3dnormal){this.entity=entity;this.basePoint=basePoint;this.normal=normal;}protectedoverrideSamplerStatusSampler(JigPromptsprompts){varoptions=newJigPromptAngleOptions("\nSpecify rotation angle: ");options.BasePoint=basePoint;options.UseBasePoint=true;options.Cursor=CursorType.RubberBand;options.UserInputControls=UserInputControls.Accept3dCoordinates|UserInputControls.UseBasePointElevation;varresult=prompts.AcquireAngle(options);if(result.Value==rotation)returnSamplerStatus.NoChange;rotation=result.Value;returnSamplerStatus.OK;}protectedoverrideboolWorldDraw(AcGi.WorldDrawdraw){AcGi.WorldGeometrygeom=draw.Geometry;if(geom!=null){geom.PushModelTransform(Matrix3d.Rotation(rotation,normal,basePoint));geom.Draw(entity);geom.PopModelTransform();}returntrue;}}}