雪山飞狐_lzh
发表于 2012-7-16 11:02:52
本帖最后由 雪山飞狐_lzh 于 2012-7-17 08:12 编辑
/// <summary>
/// 单行文字曲线分布,简单的示例
/// </summary>
public static void Test4()
{
var ed = SystemManager.Editor;
var opts = new PromptEntityOptions("\n请选择文本:");
opts.SetRejectMessage("你选择的不是文本,请重新选择!");
opts.AddAllowedClass(typeof(DBText), true);
var res1 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
opts = new PromptEntityOptions("\n请选择曲线:");
opts.SetRejectMessage("你选择的不是曲线,请重新选择!");
opts.AddAllowedClass(typeof(Curve), false);
var res2 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
using (var tr = new DBTransaction())
{
var btr = tr.OpenCurrentSpace(OpenMode.ForWrite);
var txt = tr.GetObject<DBText>(res1.ObjectId);
var ext = txt.GeometricExtents;
var txts = new List<DBText>();
int color = 1;
var curve = tr.GetObject<Curve>(res2.ObjectId);
double w = 0;
foreach (char c in txt.TextString)
{
var s = c.ToString();
var txtx =
new DBText
{
TextString = s,
TextStyleId = txt.TextStyleId,
Height = txt.Height,
ColorIndex = color++
};
txtx.SetDatabaseDefaults();
txts.Add(txtx);
var width = AcUtils.GetTextExtents(txt.TextStyleId, s, txt.Height).X;
w += width / 2;
txtx.Justify = AttachmentPoint.BottomCenter;
txtx.AlignmentPoint = curve.GetPointAtDist(w);
txtx.Rotation =
curve
.GetFirstDerivative(curve.GetParameterAtDistance(w))
.GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis);
w += width / 2;
}
tr.AddEntity(btr, txts);
}
}
雪山飞狐_lzh
发表于 2012-7-17 22:08:07
本帖最后由 雪山飞狐_lzh 于 2012-7-17 22:36 编辑
/// <summary>
/// 单行文字曲线分布+拖动
/// </summary>
public static void Test4()
{
var ed = SystemManager.Editor;
var opts = new PromptEntityOptions("\n请选择文本:");
opts.SetRejectMessage("你选择的不是文本,请重新选择!");
opts.AddAllowedClass(typeof(DBText), true);
var res1 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
opts = new PromptEntityOptions("\n请选择曲线:");
opts.SetRejectMessage("你选择的不是曲线,请重新选择!");
opts.AddAllowedClass(typeof(Curve), false);
var res2 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
using (var tr = new DBTransaction())
{
var txt = tr.GetObject<DBText>(res1.ObjectId);
var ext = txt.GeometricExtents;
var txts = new List<DBText>();
var dists = new List<double>();
var curve = tr.GetObject<Curve>(res2.ObjectId);
double w = 0;
foreach (char c in txt.TextString)
{
var s = c.ToString();
var txtx =
new DBText
{
Height = txt.Height,
TextStyleId = txt.TextStyleId,
TextString = s,
};
txtx.SetDatabaseDefaults();
var width = AcUtils.GetTextExtents(txt.TextStyleId, s, txt.Height).X;
txts.Add(txtx);
dists.Add(w);
w += width;
}
TextJig jig = new TextJig(txts,dists, curve);
var resdrag = ed.Drag(jig);
if (resdrag.Status == PromptStatus.OK)
tr.AddEntity(tr.OpenCurrentSpace(), txts);
}
}
private class TextJig : DrawJig
{
List<DBText> _txts;
List<double> _dists;
Curve _curve;
Point3d _pos;
public TextJig(List<DBText> txts, List<double> dists, Curve curve)
{
_txts = txts;
_dists = dists;
_curve = curve;
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
try
{
Point3d pnt = _curve.GetClosestPointTo(_pos, false);
double w = _curve.GetDistAtPoint(pnt);
for (int i = 0; i < _txts.Count; i++)
{
double dist = w + _dists;
double par = _curve.GetParameterAtDistance(dist);
_txts.Position = _curve.GetPointAtParameter(par);
_txts.Rotation = _curve.GetFirstDerivative(par).GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis);
draw.Geometry.Draw(_txts);
}
return true;
}
catch
{
return false;
}
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions("\n请输入起点:");
jigOpts.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted;
PromptPointResult res = prompts.AcquirePoint(jigOpts);
Point3d pnt = res.Value;
if (pnt != _pos)
{
_pos = pnt;
}
else
return SamplerStatus.NoChange;
if (res.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
}
雪山飞狐_lzh
发表于 2012-7-17 22:18:26
DBText在Jig里不能改变AlignmentPoint,只能改变Position或者使用TransformBy来改变位置
应该算是个一直存在的Bug吧
雪山飞狐_lzh
发表于 2012-7-18 10:43:58
本帖最后由 雪山飞狐_lzh 于 2012-7-19 12:18 编辑
/// <summary>
/// 单行文字曲线分布+拖动
/// </summary>
public static void TextByCurve()
{
var ed = SystemManager.Editor;
var opts = new PromptEntityOptions("\n请选择文本:");
opts.SetRejectMessage("你选择的不是文本,请重新选择!");
opts.AddAllowedClass(typeof(DBText), true);
var res1 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
opts = new PromptEntityOptions("\n请选择曲线:");
opts.SetRejectMessage("你选择的不是曲线,请重新选择!");
opts.AddAllowedClass(typeof(Curve), false);
var res2 = ed.GetEntity(opts);
if (res1.Status != PromptStatus.OK) return;
using (var tr = new DBTransaction())
{
var txt = tr.GetObject<DBText>(res1.ObjectId);
var curve = tr.GetObject<Curve>(res2.ObjectId);
double w = 0;
var txts = new List<DBText>();
var dists = new List<double>();
foreach (char c in txt.TextString)
{
var s = c.ToString();
var txtx =
new DBText
{
Height = txt.Height,
TextStyleId = txt.TextStyleId,
TextString = s,
};
txtx.SetDatabaseDefaults();
var width = AcUtils.GetTextExtents(txt.TextStyleId, s, txt.Height).X;
txts.Add(txtx);
dists.Add(w);
w += width;
}
TextJig jig = new TextJig(txts, dists, curve);
var resdrag = ed.Drag(jig);
if (resdrag.Status == PromptStatus.OK)
tr.AddEntity(tr.OpenCurrentSpace(), txts);
}
}
class TextJig : DrawJig
{
List<DBText> _txts;
List<double> _dists;
Curve3d _curve;
double _len;
Point3d _pos;
public TextJig(List<DBText> txts, List<double> dists, Curve curve)
{
_txts = txts;
_dists = dists;
_curve = curve.ToCurve3d();
var interval = _curve.GetInterval();
_len = _curve.GetLength(0, interval.UpperBound, 0);
}
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
try
{
var poc = _curve.GetClosestPointTo(_pos, Tolerance.Global);
Point3d pnt = poc.Point;
double w = _curve.GetLength(0, poc.Parameter, 0);
double n = 1;
var vec = poc.GetDerivative(1);
if ((_pos - pnt).GetAngleTo(vec, Vector3d.ZAxis) < Math.PI)
n = -1;
for(int i = 0; i< _txts.Count; i++)
{
var txtx = _txts;
double dist = w + n * _dists;
if (dist < 0 || dist > _len)
{
break;
}
else
{
poc.Parameter = _curve.GetParameterAtLength(0, dist, true, Tolerance.Global.EqualPoint);
txtx.Position = poc.Point;
vec = poc.GetDerivative(1) * n;
txtx.Rotation = vec.GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis);
draw.Geometry.Draw(txtx);
}
}
return true;
}
catch
{
return false;
}
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions("\n请输入起点:");
jigOpts.UserInputControls =
UserInputControls.Accept3dCoordinates |
UserInputControls.NoZeroResponseAccepted |
UserInputControls.NoNegativeResponseAccepted;
PromptPointResult res = prompts.AcquirePoint(jigOpts);
Point3d pnt = res.Value;
if (pnt != _pos)
{
_pos = pnt;
}
else
return SamplerStatus.NoChange;
if (res.Status == PromptStatus.Cancel)
return SamplerStatus.Cancel;
else
return SamplerStatus.OK;
}
}
huaxiamengqing
发表于 2012-11-2 13:01:02
雪山飞狐_lzh 发表于 2010-5-15 13:47 static/image/common/back.gif
另外贴两个去除MText格式的函数
这个方法有没有漏洞?
http://bbs.mjtd.com/thread-97634-1-1.html
huaxiamengqing
发表于 2012-11-2 17:48:31
狐哥,这个DBTransaction,我怎么找不到,在VB.net里面,是你自己写的类吗?
huaxiamengqing
发表于 2012-11-6 18:23:05
翻译了两天终于翻译出来了- - 下面是VB版的。狐哥怎么那么忙,都不搭理我一下,要引用TlsBasal.dll 这个dll狐哥贴了好几个了。将此dll的复制本地属性设置为true.下面是代码
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports TlsCad.Trans
Public Class Mcls
<CommandMethod("td")> _
Public Shared Sub Test4()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim opts As PromptEntityOptions = New PromptEntityOptions(vbCr & "请选择文本:")
opts.SetRejectMessage(vbCr & "你选择的不是文本,请重新选择!")
opts.AddAllowedClass(GetType(DBText), True)
Dim res1 As PromptEntityResult = ed.GetEntity(opts)
If res1.Status <> PromptStatus.OK Then
Return
End If
opts = New PromptEntityOptions(vbCr & "请选择曲线:")
opts.SetRejectMessage(vbCr & "你选择的不是曲线,请重新选择!")
opts.AddAllowedClass(GetType(Curve), False)
Dim res2 As PromptEntityResult = ed.GetEntity(opts)
If res1.Status <> PromptStatus.OK Then
Return
End If
Using tr As New DBTransaction
Dim txt As DBText = CType(tr.GetObject(res1.ObjectId), DBText)
Dim ext As Extents3d = txt.GeometricExtents
Dim txts As New List(Of DBText)
Dim dists As New List(Of Double)
Dim curve As DBObject = tr.GetObject(res2.ObjectId)
Dim w As Double = 0
Dim c As Char
For Each c In txt.TextString
Dim s As String = c.ToString()
Dim txtx As New DBText
txtx.Height = txt.Height
txtx.TextStyleId = txt.TextStyleId
txtx.TextString = s
txtx.SetDatabaseDefaults()
Dim width As Double = Autodesk.AutoCAD.Internal.Utils.GetTextExtents(txt.TextStyleId, s, txt.Height).X
txts.Add(txtx)
dists.Add(w)
w += width
Next
Dim jig As TextJig = New TextJig(txts, dists, curve)
Dim resdrag As PromptPointResult = ed.Drag(jig)
If resdrag.Status = PromptStatus.OK Then
tr.AddEntity(tr.OpenCurrentSpace(), txts)
End If
End Using
End Sub
End Class
Public Class TextJig
Inherits DrawJig
Dim _txts As List(Of DBText)
Dim _dists As List(Of Double)
Dim _curve As Curve3d
Dim _len As Double
Dim _pos As Point3d
Public Sub New(ByVal txts As List(Of DBText), ByVal dists As List(Of Double), ByVal curve As Curve)
_txts = txts
_dists = dists
_curve = TlsCad.ExtendMethods.CurveEx.ToCurve3d(curve)
Dim interval As Interval = _curve.GetInterval()
_len = _curve.GetLength(0, interval.UpperBound, 0)
End Sub
Protected Overrides Function WorldDraw(ByVal draw As Autodesk.AutoCAD.GraphicsInterface.WorldDraw) As Boolean
Try
Dim poc As PointOnCurve3d = _curve.GetClosestPointTo(_pos, Tolerance.Global)
Dim pnt As Point3d = poc.GetPoint
Dim w As Double = _curve.GetLength(0, poc.Parameter, 0)
Dim n As Double = 1
Dim vec As Vector3d = poc.GetDerivative(1)
If (_pos - pnt).GetAngleTo(vec, Vector3d.ZAxis) < Math.PI Then
n = -1
End If
Dim i As Integer
For i = 0 To _txts.Count - 1 Step i + 1
Dim txtx As DBText = _txts(i)
Dim dist As Double = w + n * _dists(i)
If dist < 0 Or dist > _len Then
Exit For
Else
poc.Parameter = _curve.GetParameterAtLength(0, dist, True, Tolerance.Global.EqualPoint)
txtx.Position = poc.Point
vec = poc.GetDerivative(1) * n
txtx.Rotation = vec.GetAngleTo(Vector3d.XAxis, -Vector3d.ZAxis)
draw.Geometry.Draw(txtx)
End If
Next
Return True
Catch
Return False
End Try
End Function
Protected Overrides Function Sampler(ByVal prompts As JigPrompts) As SamplerStatus
Dim jigOpts As JigPromptPointOptions = New JigPromptPointOptions(vbCr & "请输入起点:")
jigOpts.UserInputControls = _
UserInputControls.Accept3dCoordinates Or UserInputControls.NoZeroResponseAccepted Or _
UserInputControls.NoNegativeResponseAccepted
Dim res As PromptPointResult = prompts.AcquirePoint(jigOpts)
Dim pnt As Point3d = res.Value
If pnt <> _pos Then
_pos = pnt
Else
Return SamplerStatus.NoChange
End If
If res.Status = PromptStatus.Cancel Then
Return SamplerStatus.Cancel
Else
Return SamplerStatus.OK
End If
End Function
End Class
huaxiamengqing
发表于 2012-11-6 18:24:23
我还会继续翻译其他的C#代码,希望狐哥能帮助下。VB的资料太少了- -
SWAYWOOD
发表于 2013-8-14 13:11:12
YAOSHIWEI 发表于 2012-6-5 13:08 static/image/common/back.gif
楼主,可以不可以来个lisp的版本
lisp过时了
mycad
发表于 2013-10-26 09:44:13
顶!!!!!!!!!!!