Chuyển đến nội dung
Diễn đàn CADViet
Đăng nhập để thực hiện theo  
Detailing

Jigging trong .NET

Các bài được khuyến nghị

Tiếp theo topic Draw overrule mình giới thiệu tiếp công cụ thứ 2 mà AutoCAD .NET cung cấp cho ng dùng để việc tương tác được dễ dàng hơn: Class Jig

theo đó có 2 cách: kế thừa lớp DrawJig hoặc EntityJig. về cơ bản thì cũng tương tự nhau.

file dưới đây mình dùng DrawJig.

83237_jigarea_1.gif

Download Here: http://www.cadviet.c...37_hatchjig.zip

Chú ý: chỉ chạy trên CAD10 trở lên 32bit.

Description:

Giải nén và load vào AutoCAD bằng lệnh
NETLOAD

Lệnh
JA

 

Enjoy!

  • Vote tăng 2

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Detailing xin cho hỏi:

- Khái niệm Jig (vì thấy một vài nơi sử dụng nhưng không hiểu nói về vấn đề gì)

- Cách để tạo một Polyline chứa Line và Arc trong AutoCad.NET

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Detailing xin cho hỏi:

- Khái niệm Jig (vì thấy một vài nơi sử dụng nhưng không hiểu nói về vấn đề gì)

- Cách để tạo một Polyline chứa Line và Arc trong AutoCad.NET

Chào bạn,

Jig là 1 class wrapper từ AcEdJig nó có tác dụng trong việc Input nhìn cho trưc quan thôi, ví dụ như khi vẽ Polyline, insert Block, Scale, Mirror, ... Nó override method WorldDraw của editor trong quá trình input.

Đây là đoạn code tạo Polyline gồm cả Line và Arc của Kean Walmsley kế thừa lớp EnityJig

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;

namespace PolylineJig
{
 class JigUtils
 {
// Custom ArcTangent method, as the Math.Atan
// doesn't handle specific cases

public static double Atan(double y, double x)
{
 	if (x > 0)
   	return Math.Atan(y / x);
 	else if (x < 0)
   	return Math.Atan(y / x) - Math.PI;
 	else  // x == 0
 	{
   	if (y > 0)
     	return Math.PI;
   	else if (y < 0)
     	return -Math.PI;
   	else // if (y == 0) theta is undefined
     	return 0.0;
 	}
}

// Computes Angle between current direction
// (vector from last vertex to current vertex)
// and the last pline segment

public static double ComputeAngle(
 	Point3d startPoint, Point3d endPoint,
 	Vector3d xdir, Matrix3d ucs
)
{
 	Vector3d v =
   	new Vector3d(
     	(endPoint.X - startPoint.X) / 2,
     	(endPoint.Y - startPoint.Y) / 2,
     	(endPoint.Z - startPoint.Z) / 2
   	);

 	double cos = v.DotProduct(xdir);
 	double sin =
   	v.DotProduct(
     	Vector3d.ZAxis.TransformBy(ucs).CrossProduct(xdir)
   	);

 	return Atan(sin, cos);
}
 }

 public class BulgePolyJig : EntityJig
 {
Point3d _tempPoint;
Plane _plane;
bool _isArcSeg = false;
bool _isUndoing = false;
Matrix3d _ucs;

public BulgePolyJig(Matrix3d ucs) : base(new Polyline())
{
 	_ucs = ucs;
 	Vector3d normal = Vector3d.ZAxis.TransformBy(ucs);
 	_plane = new Plane(Point3d.Origin, normal);
 	Polyline pline = Entity as Polyline;
 	pline.SetDatabaseDefaults();
 	pline.Normal = normal;
 	AddDummyVertex();
}

protected override SamplerStatus Sampler(JigPrompts prompts)
{
 	JigPromptPointOptions jigOpts = new JigPromptPointOptions();

 	jigOpts.UserInputControls =
   	(UserInputControls.Accept3dCoordinates |
   	UserInputControls.NullResponseAccepted |
   	UserInputControls.NoNegativeResponseAccepted);

 	_isUndoing = false;

 	Polyline pline = Entity as Polyline;

 	if (pline.NumberOfVertices == 1)
 	{
   	// For the first vertex, just ask for the point

   	jigOpts.Message = "\nSpecify start point: ";
 	}
 	else if (pline.NumberOfVertices > 1)
 	{
   	string msgAndKwds =
     	(_isArcSeg ?
       	"\nSpecify endpoint of arc or [Line/Undo]: " :
       	"\nSpecify next point or [Arc/Undo]: "
     	);

   	string kwds = (_isArcSeg ? "Line Undo" : "Arc Undo");

   	jigOpts.SetMessageAndKeywords(msgAndKwds, kwds);
 	}
 	else
   	return SamplerStatus.Cancel; // Should never happen

 	// Get the point itself

 	PromptPointResult res = prompts.AcquirePoint(jigOpts);

 	if (res.Status == PromptStatus.Keyword)
 	{
   	if (res.StringResult.ToUpper() == "ARC")
     	_isArcSeg = true;
   	else if (res.StringResult.ToUpper() == "LINE")
     	_isArcSeg = false;
   	else if (res.StringResult.ToUpper() == "UNDO")
     	_isUndoing = true;

   	return SamplerStatus.OK;
 	}
 	else if (res.Status == PromptStatus.OK)
 	{
   	// Check if it has changed or not (reduces flicker)

   	if (_tempPoint == res.Value)
     	return SamplerStatus.NoChange;
   	else
   	{
     	_tempPoint = res.Value;
     	return SamplerStatus.OK;
   	}
 	}

 	return SamplerStatus.Cancel;
}

protected override bool Update()
{
 	// Update the dummy vertex to be our 3D point
 	// projected onto our plane

 	Polyline pl = Entity as Polyline;

 	if (_isArcSeg)
 	{
   	Point3d lastVertex =
     	pl.GetPoint3dAt(pl.NumberOfVertices - 2);

   	Vector3d refDir;

   	if (pl.NumberOfVertices < 3)
     	refDir = new Vector3d(1.0, 1.0, 0.0);
   	else
   	{
     	// Check bulge to see if last segment was an arc or a line

     	if (pl.GetBulgeAt(pl.NumberOfVertices - 3) != 0)
     	{
       	CircularArc3d arcSegment =
         	pl.GetArcSegmentAt(pl.NumberOfVertices - 3);

       	Line3d tangent = arcSegment.GetTangent(lastVertex);

       	// Reference direction is the invert of the arc tangent
       	// at last vertex

       	refDir = tangent.Direction.MultiplyBy(-1.0);
     	}
     	else
     	{
       	Point3d pt =
         	pl.GetPoint3dAt(pl.NumberOfVertices - 3);

       	refDir =
         	new Vector3d(
           	lastVertex.X - pt.X,
           	lastVertex.Y - pt.Y,
           	lastVertex.Z - pt.Z
         	);
     	}
   	}

   	double angle =
     	JigUtils.ComputeAngle(
       	lastVertex, _tempPoint, refDir, _ucs
     	);

   	// Bulge is defined as tan of one fourth of included angle
   	// Need to double the angle since it represents the included
   	// angle of the arc
   	// So formula is: bulge = Tan(angle * 2 * 0.25)

   	double bulge = Math.Tan(angle * 0.5);

   	pl.SetBulgeAt(pl.NumberOfVertices - 2, bulge);
 	}
 	else
 	{
   	// Line mode. Need to remove last bulge if there was one

   	if (pl.NumberOfVertices > 1)
     	pl.SetBulgeAt(pl.NumberOfVertices - 2, 0);
 	}

 	pl.SetPointAt(
   	pl.NumberOfVertices - 1, _tempPoint.Convert2d(_plane)
 	);

 	return true;
}

public bool IsUndoing
{
 	get
 	{
   	return _isUndoing;
 	}
}

public void AddDummyVertex()
{
 	// Create a new dummy vertex... can have any initial value

 	Polyline pline = Entity as Polyline;
 	pline.AddVertexAt(
   	pline.NumberOfVertices, new Point2d(0, 0), 0, 0, 0
 	);
}

public void RemoveLastVertex()
{
 	Polyline pline = Entity as Polyline;

 	// Let's first remove our dummy vertex  

 	if (pline.NumberOfVertices > 0)
   	pline.RemoveVertexAt(pline.NumberOfVertices - 1);

 	// And then check the type of the last segment

 	if (pline.NumberOfVertices >= 2)
 	{
   	double blg = pline.GetBulgeAt(pline.NumberOfVertices - 2);
   	_isArcSeg = (blg != 0);
 	}
}

public void Append()
{
 	Database db = HostApplicationServices.WorkingDatabase;

 	Transaction tr =
   	db.TransactionManager.StartTransaction();
 	using (tr)
 	{
   	BlockTable bt =
     	tr.GetObject(
       	db.BlockTableId, OpenMode.ForRead
     	) as BlockTable;
   	BlockTableRecord btr =
     	tr.GetObject(
       	bt[blockTableRecord.ModelSpace], OpenMode.ForWrite
     	) as BlockTableRecord;

   	btr.AppendEntity(this.Entity);
   	tr.AddNewlyCreatedDBObject(this.Entity, true);
   	tr.Commit();
 	}
}

[CommandMethod("BPJIG")]
public static void RunBulgePolyJig()
{
 	Document doc = Application.DocumentManager.MdiActiveDocument;
 	Database db = doc.Database;
 	Editor ed = doc.Editor;

 	BulgePolyJig jig =
   	new BulgePolyJig(ed.CurrentUserCoordinateSystem);

 	while (true)
 	{
   	PromptResult res = ed.Drag(jig);

   	switch (res.Status)
   	{
     	// New point was added, keep going

     	case PromptStatus.OK:
       	jig.AddDummyVertex();
       	break;

     	// Keyword was entered

     	case PromptStatus.Keyword:
       	if (jig.IsUndoing)
         	jig.RemoveLastVertex();
       	break;

     	// If the jig completed successfully, add the polyline

     	case PromptStatus.None:
       	jig.RemoveLastVertex();
       	jig.Append();
       	return;

     	// User cancelled the command, get out of here
     	// and don't forget to dispose the jigged entity

     	default:
       	jig.Entity.Dispose();
       	return;
   	}
 	}
}
 }
}

  • Vote tăng 2

Chia sẻ bài đăng này


Liên kết tới bài đăng
Chia sẻ trên các trang web khác

Tạo một tài khoản hoặc đăng nhập để nhận xét

Bạn cần phải là một thành viên để lại một bình luận

Tạo tài khoản

Đăng ký một tài khoản mới trong cộng đồng của chúng tôi. Điều đó dễ mà.

Đăng ký tài khoản mới

Đăng nhập

Bạn có sẵn sàng để tạo một tài khoản ? Đăng nhập tại đây.

Đăng nhập ngay
Đăng nhập để thực hiện theo  

×