publicstaticclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){try{using(Transactiontransaction=ActiveUtil.Document.TransactionManager.StartTransaction()){vartext=newCadText(newGeometry.Entities.Point2D(),"Test"){TextHeight=100,TextStyle="ARIAL"};text.Append();//Code for jigvartextJig=newInsertTextJig(text.Entity);// Loop as we run our jig, as we may have keywordsPromptStatusstat=PromptStatus.Keyword;while(stat==PromptStatus.Keyword){PromptResultjigResult=ActiveUtil.Editor.Drag(textJig);stat=jigResult.Status;if(stat!=PromptStatus.OK&&stat!=PromptStatus.Keyword){return;}}transaction.Commit();}}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}}publicclassInsertTextJig:EntityJig{privatePoint3d_position;publicInsertTextJig(Entityentity):base(entity){}// Method for user interaction with entityprotectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsjigPointPrompt=newJigPromptPointOptions("\nPosition of text");jigPointPrompt.UserInputControls=(UserInputControls.Accept3dCoordinates|UserInputControls.NullResponseAccepted|UserInputControls.NoNegativeResponseAccepted|UserInputControls.GovernedByOrthoMode);PromptPointResultppr=prompts.AcquirePoint(jigPointPrompt);if(ppr.Status==PromptStatus.OK){// Check if it has changed or not (reduces flicker)if(_position.DistanceTo(ppr.Value)<Tolerance.Global.EqualPoint){returnSamplerStatus.NoChange;}_position=ppr.Value;returnSamplerStatus.OK;}returnSamplerStatus.Cancel;}// Method to Update AutoCAD Entity depending on user interactionprotectedoverrideboolUpdate(){DBTexttxt=(DBText)Entity;txt.Position=_position;returntrue;}}
publicstaticclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){try{using(Transactiontransaction=ActiveUtil.Document.TransactionManager.StartTransaction()){varcircle=newCadCircle(newGeometry.Entities.Point2D(),50);circle.Append();//Code for jigvartextJig=newEntityPositionJig(circle.Entity);// Loop as we run our jig, as we may have keywordsPromptStatusstat=PromptStatus.Keyword;while(stat==PromptStatus.Keyword){PromptResultjigResult=ActiveUtil.Editor.Drag(textJig);stat=jigResult.Status;if(stat!=PromptStatus.OK&&stat!=PromptStatus.Keyword){return;}}transaction.Commit();}}catch(System.Exceptionex){Application.ShowAlertDialog($"Something went wrong error:{ex.Message}");}}}publicclassEntityPositionJig:EntityJig{privatePoint3d_position;publicEntityPositionJig(Entityentity):base(entity){}// Method for user interaction with entityprotectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsjigPointPrompt=newJigPromptPointOptions("\nPosition of entity");jigPointPrompt.UserInputControls=(UserInputControls.Accept3dCoordinates|UserInputControls.NullResponseAccepted|UserInputControls.NoNegativeResponseAccepted|UserInputControls.GovernedByOrthoMode);PromptPointResultppr=prompts.AcquirePoint(jigPointPrompt);if(ppr.Status==PromptStatus.OK){// Check if it has changed or not (reduces flicker)if(_position.DistanceTo(ppr.Value)<Tolerance.Global.EqualPoint){returnSamplerStatus.NoChange;}_position=ppr.Value;returnSamplerStatus.OK;}returnSamplerStatus.Cancel;}// Method to Update AutoCAD Entity depending on user interactionprotectedoverrideboolUpdate(){Circleentity=(Circle)Entity;entity.Center=_position;returntrue;}}
Code to get user input with custom autocad graphics
publicclassMain{[CommandMethod("TEST")]publicvoidTest(){vardoc=Application.DocumentManager.MdiActiveDocument;vardb=doc.Database;vared=doc.Editor;varoptions=newPromptEntityOptions("\nSelect line: ");options.SetRejectMessage("\nSelected entity is not a line.");options.AddAllowedClass(typeof(Line),true);varresult=ed.GetEntity(options);if(result.Status!=PromptStatus.OK)return;using(vartr=db.TransactionManager.StartTransaction()){varaxis=(Line)tr.GetObject(result.ObjectId,OpenMode.ForRead);using(varperp=newLine(Point3d.Origin,axis.GetClosestPointTo(Point3d.Origin,true))){varjig=newPerpendicularJig(perp,axis,ed);varpr=ed.Drag(jig);if(pr.Status==PromptStatus.OK){varcurSpace=(BlockTableRecord)tr.GetObject(db.CurrentSpaceId,OpenMode.ForWrite);curSpace.AppendEntity(perp);tr.AddNewlyCreatedDBObject(perp,true);}}tr.Commit();}}}internalclassPerpendicularJig:EntityJig{privatePoint3ddragPoint;Lineaxis,perp;privateVector3daxisDir;Editored;publicPerpendicularJig(Lineperp,Lineaxis,Editored):base(perp){this.axis=axis;this.perp=perp;axisDir=axis.StartPoint.GetVectorTo(axis.EndPoint);this.ed=ed;}protectedoverrideSamplerStatusSampler(JigPromptsprompts){varoptions=newJigPromptPointOptions("\nSpecify a point: ");options.UserInputControls=UserInputControls.Accept3dCoordinates;varresult=prompts.AcquirePoint(options);if(result.Value.IsEqualTo(dragPoint))returnSamplerStatus.NoChange;dragPoint=result.Value;returnSamplerStatus.OK;}protectedoverrideboolUpdate(){using(varview=ed.GetCurrentView()){varviewDir=view.ViewDirection;perp.StartPoint=dragPoint;perp.EndPoint=axis.GetClosestPointTo(dragPoint,viewDir,true);varxform=Matrix3d.WorldToPlane(viewDir);varv1=axisDir.ProjectTo(viewDir,viewDir);varv2=perp.EndPoint.GetVectorTo(dragPoint).ProjectTo(viewDir,viewDir);perp.ColorIndex=v1.GetAngleTo(v2,viewDir)<Math.PI?1:3;}returntrue;}}
Entity Jig to insert new Text
publicstaticclassCommands{[CommandMethod("QT")]staticpublicvoidQuickText(){PromptStringOptionspso=newPromptStringOptions("\nEnter text string");pso.AllowSpaces=true;PromptResultpr=ActiveUtil.Editor.GetString(pso);if(pr.Status!=PromptStatus.OK){return;}using(Transactiontransaction=ActiveUtil.TransactionManager.StartTransaction()){// Create the text object, set its normal and contentsDBTexttxt=newDBText{Normal=ActiveUtil.Editor.CurrentUserCoordinateSystem.CoordinateSystem3d.Zaxis,TextString=pr.StringResult};// We'll add the text to the database before jigging// it - this allows alignment adjustments to be// reflectedActiveUtil.Database.GetCurrentSpace(OpenMode.ForWrite).AppendEntity(txt);transaction.AddNewlyCreatedDBObject(txt,true);// Create our jigTextPlacementJigpj=newTextPlacementJig(txt);// Loop as we run our jig, as we may have keywordsPromptStatusstat=PromptStatus.Keyword;while(stat==PromptStatus.Keyword){PromptResultres=ActiveUtil.Editor.Drag(pj);stat=res.Status;if(stat!=PromptStatus.OK&&stat!=PromptStatus.Keyword){return;}}transaction.Commit();}}}internalclassTextPlacementJig:EntityJig{// Declare some internal stateprivatePoint3d_position;privatedouble_angle,_txtSize;// ConstructorpublicTextPlacementJig(Entityent):base(ent){_angle=0;_txtSize=1;}protectedoverrideSamplerStatusSampler(JigPromptsjp){// We acquire a point but with keywordsJigPromptPointOptionspo=newJigPromptPointOptions("\nPosition of text");po.UserInputControls=(UserInputControls.Accept3dCoordinates|UserInputControls.NullResponseAccepted|UserInputControls.NoNegativeResponseAccepted|UserInputControls.GovernedByOrthoMode);po.SetMessageAndKeywords("\nSpecify position of text or [Bold/Italic/Larger/Smaller/Rotate90]: ","Bold Italic Larger Smaller Rotate90");PromptPointResultppr=jp.AcquirePoint(po);if(ppr.Status==PromptStatus.Keyword){switch(ppr.StringResult){case"Bold":{// TODObreak;}case"Italic":{// TODObreak;}case"Larger":{// Multiple the text size by two_txtSize*=2;break;}case"Smaller":{// Divide the text size by two_txtSize/=2;break;}case"Rotate90":{// To rotate clockwise we subtract 90 degrees and// then normalise the angle between 0 and 360_angle-=Math.PI/2;while(_angle<Math.PI*2){_angle+=Math.PI*2;}break;}}returnSamplerStatus.OK;}elseif(ppr.Status==PromptStatus.OK){// Check if it has changed or not (reduces flicker)if(_position.DistanceTo(ppr.Value)<Tolerance.Global.EqualPoint){returnSamplerStatus.NoChange;}_position=ppr.Value;returnSamplerStatus.OK;}returnSamplerStatus.Cancel;}protectedoverrideboolUpdate(){// Set properties on our text objectDBTexttxt=(DBText)Entity;txt.Position=_position;txt.Height=_txtSize;txt.Rotation=_angle;returntrue;}}
Sample Code to Draw Line using Entity Jig
publicstaticclassCommands{[CommandMethod("Test")]publicstaticvoidTestEntityJigger12_Method(){if(LineJigger.Jig()){ActiveUtil.Editor.WriteMessage("\nA line segment has been successfully jigged and added to the database.\n");}else{ActiveUtil.Editor.WriteMessage("\nIt failed to jig and add a line segment to the database.\n");}}}publicclassLineJigger:EntityJig{publicPoint3dmEndPoint=newPoint3d();publicLineJigger(Lineent):base(ent){}protectedoverrideboolUpdate(){(EntityasLine).EndPoint=mEndPoint;returntrue;}protectedoverrideSamplerStatusSampler(JigPromptsprompts){JigPromptPointOptionsprOptions1=newJigPromptPointOptions("\nNext point:");prOptions1.BasePoint=(EntityasLine).StartPoint;prOptions1.UseBasePoint=true;prOptions1.UserInputControls=UserInputControls.Accept3dCoordinates|UserInputControls.AnyBlankTerminatesInput|UserInputControls.GovernedByOrthoMode|UserInputControls.GovernedByUCSDetect|UserInputControls.UseBasePointElevation|UserInputControls.InitialBlankTerminatesInput|UserInputControls.NullResponseAccepted;PromptPointResultprResult1=prompts.AcquirePoint(prOptions1);if(prResult1.Status==PromptStatus.Cancel)returnSamplerStatus.Cancel;if(prResult1.Value.Equals(mEndPoint)){returnSamplerStatus.NoChange;}else{mEndPoint=prResult1.Value;returnSamplerStatus.OK;}}publicstaticboolJig(){try{Databasedb=HostApplicationServices.WorkingDatabase;PromptPointResultppr=ActiveUtil.Editor.GetPoint("\nStart point");if(ppr.Status!=PromptStatus.OK)returnfalse;Point3dpt=ppr.Value;Lineent=newLine(pt,pt);ent.TransformBy(ActiveUtil.Editor.CurrentUserCoordinateSystem);LineJiggerjigger=newLineJigger(ent);PromptResultpr=ActiveUtil.Editor.Drag(jigger);if(pr.Status==PromptStatus.OK){using(Transactiontr=db.TransactionManager.StartTransaction()){BlockTablebt=(BlockTable)tr.GetObject(db.BlockTableId,OpenMode.ForRead);BlockTableRecordbtr=(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);btr.AppendEntity(jigger.Entity);tr.AddNewlyCreatedDBObject(jigger.Entity,true);tr.Commit();}}else{ent.Dispose();returnfalse;}returntrue;}catch{returnfalse;}}}
Sample Code to Draw entire circle using entity jig
publicstaticclassCommands{[CommandMethod("Test", CommandFlags.UsePickSet)]publicstaticvoidTest(){if(CircleJig.Jig()){ActiveUtil.Editor.WriteLine("A circle has been successfully jigged and added to the database.\n");}else{ActiveUtil.Editor.WriteLine("\nIt failed to jig and add a circle to the database.\n");}}}publicclassCircleJig:EntityJig{privateCircle_circle;privatePoint3d_centerPoint=newPoint3d();privatedouble_radius=0.001;publicint_mCurJigFactorNumber=1;publicCircleJig(Entityent):base(ent){_circle=(Circle)ent;_circle.Radius=_radius;_circle.Center=_centerPoint;}internalstaticboolJig(){try{Circleent=newCircle();CircleJigjigger=newCircleJig(ent);PromptResultpr;do{pr=ActiveUtil.Editor.Drag(jigger);jigger._mCurJigFactorNumber++;}while(pr.Status!=PromptStatus.Cancel&&jigger._mCurJigFactorNumber<=2);if(pr.Status!=PromptStatus.Cancel){using(Transactiontr=ActiveUtil.TransactionManager.StartTransaction()){ActiveUtil.Database.GetModelSpace(OpenMode.ForWrite).AppendEntity(jigger.Entity);tr.AddNewlyCreatedDBObject(jigger.Entity,true);tr.Commit();}}}catch{returnfalse;}returntrue;}protectedoverrideSamplerStatusSampler(JigPromptsprompts){switch(_mCurJigFactorNumber){case1:JigPromptPointOptionsprOptions1=newJigPromptPointOptions("\nCircle center:");PromptPointResultprResult1=prompts.AcquirePoint(prOptions1);if(prResult1.Status==PromptStatus.Cancel)returnSamplerStatus.Cancel;if(prResult1.Value.Equals(_centerPoint)){returnSamplerStatus.NoChange;}else{_centerPoint=prResult1.Value;returnSamplerStatus.OK;}case2:JigPromptDistanceOptionsprOptions2=newJigPromptDistanceOptions("\nCircle radius:");prOptions2.BasePoint=_centerPoint;PromptDoubleResultprResult2=prompts.AcquireDistance(prOptions2);if(prResult2.Status==PromptStatus.Cancel)returnSamplerStatus.Cancel;if(prResult2.Value.Equals(_radius)){returnSamplerStatus.NoChange;}else{if(prResult2.Value<0.0001)// To avoid the degeneration problem!{returnSamplerStatus.NoChange;}else{_radius=prResult2.Value;returnSamplerStatus.OK;}}default:break;}returnSamplerStatus.OK;}protectedoverrideboolUpdate(){switch(_mCurJigFactorNumber){case1:(EntityasCircle).Center=_centerPoint;break;case2:(EntityasCircle).Radius=_radius;break;default:returnfalse;}returntrue;}}