.NET中如何把圆转成多义线?
本帖最后由 SUGAR1122 于 2011-1-5 10:19 编辑如何把圆转成多义线? 这样可以控制圆的线宽。
没查到相关信息,在CAD命令下可以直接用ployline,选圆弧,输入直径,再选择原来的起点,好像生成的圆不是真正的圆,用bo命令可以生成,感觉不是很恰当,也没法用.net控制。
用.NET有什么其他方法吗?
就用polyline生成两节圆弧 我看到别的程序生成的好像不是2个圆弧啊,不知道怎么做的,圆环? 同楼上观点,顺便给你写个代码吧:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace CADTest
{
public class Class1
{
public void CircleToPloyline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction trans = db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
PromptSelectionResult psr = ed.GetSelection();
//获取选择集,这里就不过滤了
SelectionSet ss = null;
if (psr.Status == PromptStatus.OK)
{
ss = psr.Value;
foreach (SelectedObject so in ss)
{
Circle c = trans.GetObject(so.ObjectId, OpenMode.ForWrite) as Circle;
double r = c.Radius;
Point3d cc = c.Center;
Point2d p1 = new Point2d(cc.X + r, cc.Y);
Point2d p2 = new Point2d(cc.X - r, cc.Y);
Polyline poly = new Polyline();
poly.AddVertexAt(0, p1, 1, 0, 0);
poly.AddVertexAt(1, p2, 1, 0, 0);
poly.AddVertexAt(2, p1, 1, 0, 0);
btr.AppendEntity(poly);
trans.AddNewlyCreatedDBObject(poly, true);
c.Erase(true);
}
}
trans.Commit();
trans.Dispose();
}
public void GetEntityType()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("请选择一个实体");
PromptEntityResult per = null;
try
{
per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
ObjectId id = per.ObjectId;
Transaction trans = db.TransactionManager.StartTransaction();
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, true);
ed.WriteMessage("\n实体ObjectId为:" + ent.ObjectId + "\n实体类型为:" + ent.GetType().FullName);
trans.Commit();
trans.Dispose();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception exc)
{
ed.WriteMessage("发生异常,原因为:" + exc.Message);
}
}
}
} 本帖最后由 cdinten 于 2011-1-5 22:06 编辑
这个是效果图:
如果看不了,可以上我的博客看,地址在这里 非常感谢cdinten ,提供代码和效果,效果很好,谢谢 本帖最后由 fdafsaf 于 2011-9-18 23:51 编辑
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
namespace CADTest
{
public class Class1
{
public void CircleToPloyline()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction trans = db.TransactionManager.StartTransaction();
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
PromptSelectionResult psr = ed.GetSelection();
//获取选择集,这里就不过滤了
SelectionSet ss = null;
if (psr.Status == PromptStatus.OK)
{
ss = psr.Value;
foreach (SelectedObject so in ss)
{
Circle c = trans.GetObject(so.ObjectId, OpenMode.ForWrite) as Circle;
double r = c.Radius;
Point3d cc = c.Center;
Point2d p1 = new Point2d(cc.X + r, cc.Y);
Point2d p2 = new Point2d(cc.X - r, cc.Y);
Polyline poly = new Polyline();
poly.AddVertexAt(0, p1, 1, 0, 0);
poly.AddVertexAt(1, p2, 1, 0, 0);
poly.AddVertexAt(2, p1, 1, 0, 0);
btr.AppendEntity(poly);
trans.AddNewlyCreatedDBObject(poly, true);
c.Erase(true);
}
}
trans.Commit();
trans.Dispose();
}
public void GetEntityType()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions peo = new PromptEntityOptions("请选择一个实体");
PromptEntityResult per = null;
try
{
per = ed.GetEntity(peo);
if (per.Status == PromptStatus.OK)
{
ObjectId id = per.ObjectId;
Transaction trans = db.TransactionManager.StartTransaction();
Entity ent = (Entity)trans.GetObject(id, OpenMode.ForRead, true);
ed.WriteMessage("\n实体ObjectId为:" + ent.ObjectId + "\n实体类型为:" + ent.GetType().FullName);
trans.Commit();
trans.Dispose();
}
}
catch (Autodesk.AutoCAD.Runtime.Exception exc)
{
ed.WriteMessage("发生异常,原因为:" + exc.Message);
}
}
}
}
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>HTML5每日一练之Canvas标签的应用-坐标变换与路径结合使用</title>
<script type="text/javascript">
window.onload = function()
{
createPic();
}
//创建图形
function createPic()
{
var canvas = document.getElementById("W3Cfuns_canvas");
var context = canvas.getContext("2d");
context.fillStyle = "#d4d4d4";
context.fillRect(0, 0, 400, 300);
//绘制图形
context.translate(200, 50);
for(var i = 0; i < 50; i++)
{
context.translate(25, 25);
context.scale(0.95, 0.95);
context.rotate(Math.PI / 8);
createStar(context);//此方法专门绘制五角星
context.fill();
}
}
//创建五角星的方法
function createStar(c)
{
var n = 0;
var dx = 100;
var dy = 0;
var s = 50;
var x = Math.sin(0);
var y = Math.cos(0);
var dig = Math.PI / 5 * 4;
//创建路径
c.beginPath();
c.fillStyle = toRGB(parseInt(Math.random()*(255 - 0 + 1) + 0),parseInt(Math.random()*(255 - 0 + 1) + 0),parseInt(Math.random()*(255 - 0 + 1) + 0));
for(var i = 0; i < 5; i++)
{
x = Math.sin(i * dig);
y = Math.cos(i * dig);
c.lineTo(dx + x * s, dy + y * s);
}
c.closePath();
}
//小于10补零
function addZero(string){return string.length == 2 ? string : '0' + string;}
//随即颜色
function toRGB(redValue, greenValue, blueValue)
{
var
rgbR = addZero(redValue.toString(16), 2),
rgbG = addZero(greenValue.toString(16), 2),
rgbB = addZero(blueValue.toString(16), 2);
var rgb = "#" + rgbR + rgbG + rgbB;
return rgb;
}
</script>
</head>
<body>
<canvas id="W3Cfuns_canvas" width="400" height="300"></canvas>
</body>
</html> 本帖最后由 机灵鼠 于 2012-3-7 19:40 编辑
页:
[1]