乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
楼主: Bryco

在矩形的中点创建多行文字

[复制链接]

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-30 12:31:21 | 显示全部楼层
我明白为什么了,我从来没有完全使用VBA,那里的编码很难看…
这是我刚刚在C#中做的一些事情,非常简单,这可能会帮助你,你只需选择一个矩形区域,例程就会在其中放置一个文本。HTH
  1. using System;
  2. using Autodesk.AutoCAD.Runtime;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Text;
  7. using System.IO;
  8. using Autodesk.AutoCAD.ApplicationServices;
  9. using Autodesk.AutoCAD.DatabaseServices;
  10. using Autodesk.AutoCAD.Windows;
  11. using Autodesk.AutoCAD.Geometry;
  12. using Autodesk.AutoCAD.EditorInput;
  13. using System.Text.RegularExpressions;
  14. using System.Runtime.InteropServices;
  15. using System.Threading;
  16. using System.Globalization;
  17. using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  18. using AcAp = Autodesk.AutoCAD.ApplicationServices;
  19. using AcEd = Autodesk.AutoCAD.EditorInput;
  20. using AcGe = Autodesk.AutoCAD.Geometry;
  21. using AcRx = Autodesk.AutoCAD.Runtime;
  22. using AcDb = Autodesk.AutoCAD.DatabaseServices;
  23. using AcWd = Autodesk.AutoCAD.Windows;
辅助功能:
  1.         public static double Angle(Point3d pt1, Point3d pt2)
  2.         {
  3.             return Math.Atan2((pt2.Y - pt1.Y), (pt2.X - pt1.X));
  4.         }
  5.         public static Point3d Polar(Point3d ptBase, double angle, double distance)
  6.         {
  7.             return new Point3d(ptBase.X + (distance * Math.Cos(angle)), ptBase.Y + (distance * Math.Sin(angle)), 0.0);
  8.         }
  9.         public static double Distance(Point3d pt1, Point3d pt2)
  10.         {
  11.             return Math.Sqrt(Math.Pow(pt2.X - pt1.X, 2) + Math.Pow(pt2.Y - pt1.Y, 2));
  12.         }
命令:
  1.         [CommandMethod("PLACETEXT")]//place some text inside a rectangle area
  2.         public void placetext()
  3.         {
  4.             Document doc = acadApp.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;
  7.             PromptEntityResult res = ed.GetEntity("\nSelect the rectangle base line: ");
  8.             if (res.Status != PromptStatus.OK) return;
  9.             using (Transaction tr = db.TransactionManager.StartTransaction())
  10.             {
  11.                 Polyline poly = tr.GetObject(res.ObjectId, OpenMode.ForRead, false) as Polyline;
  12.                 if (poly != null)
  13.                 {
  14.                     Point3d pickPoint = res.PickedPoint;
  15.                     Point3d oPoint = poly.GetClosestPointTo(pickPoint, ed.GetCurrentView().ViewDirection, false);
  16.                     double param = 0;
  17.                     param = poly.GetParameterAtPoint(oPoint);
  18.                     double sparam=0, eparam=0;
  19.                     sparam = (int)param;
  20.                     eparam = sparam + 1;
  21.                     Point3d sp = poly.GetPointAtParameter(sparam);
  22.                     Point3d ep = poly.GetPointAtParameter(eparam);
  23.                     double ang = Angle(sp, ep);
  24.                     Extents3d ext = poly.GeometricExtents;
  25.                     Point3d min = ext.MinPoint;
  26.                     Point3d max = ext.MaxPoint;
  27.                     Point3d geoCtr = Polar(min, Angle(min, max), Distance(min, max) / 2.0);
  28.                     BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
  29.                     DBText txt = new DBText();
  30.                     txt.TextString = "TESTING"; //<==change to your default string value
  31.                     txt.SetDatabaseDefaults(db);
  32.                     txt.Height = Distance(min, max) / 8.0; //<==change to your default height
  33.                     txt.HorizontalMode = TextHorizontalMode.TextMid;
  34.                     txt.Rotation = ang;
  35.                     btr.AppendEntity(txt);
  36.                     tr.AddNewlyCreatedDBObject(txt, true);
  37.                     txt.AlignmentPoint = geoCtr;
  38.                 }
  39.                 tr.Commit();
  40.             }
  41.         }
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-30 13:33:31 | 显示全部楼层
这并不完全是您所需要的,但可能会有帮助,我认为您可以在那里添加部分,以获得所需边缘上的第二个点,然后计算文本的旋转角度;J#039~
  1. Option Explicit
  2. Sub AddSomeLabel()
  3. Dim varPt As Variant
  4. Dim oPoly As AcadLWPolyline
  5. Dim oEnt As AcadEntity
  6. With ThisDrawing
  7. .Utility.GetEntity oEnt, varPt, "Select polyline (pick left point on the edge)"
  8. If TypeOf oEnt Is AcadLWPolyline Then
  9. Set oPoly = oEnt
  10. Else
  11. MsgBox "Wrong entity selected"
  12. Exit Sub
  13. End If
  14. Dim txtPt As Variant
  15. txtPt = PseudoCenter(oPoly)
  16. Dim oText As AcadMText
  17. Dim txtStr As String
  18. txtStr = "Blah\PBlah\PBlah"
  19. Dim pointUCS As Variant
  20. pointUCS = .Utility.TranslateCoordinates(txtPt, acUCS, acUCS, False)
  21. Set oText = MakeMText(pointUCS, txtStr)
  22. End With
  23. End Sub
  24. Function MakeMText(txtPt As Variant, strTxt As String) As AcadMText
  25. Dim oMText As AcadMText
  26. Dim oLine As AcadLine
  27. Set oMText = ThisDrawing.ModelSpace.AddMText(txtPt, 0#, strTxt)
  28. oMText.AttachmentPoint = acAttachmentPointMiddleCenter
  29. oMText.InsertionPoint = txtPt
  30. oMText.Update
  31. Set MakeMText = oMText
  32. End Function
  33. Function PseudoCenter(oPoly As AcadLWPolyline) As Variant
  34. Dim minPt As Variant
  35. Dim maxPt As Variant
  36. oPoly.GetBoundingBox minPt, maxPt
  37. Dim centPt(2) As Double
  38. centPt(0) = (minPt(0) + maxPt(0)) / 2
  39. centPt(1) = (minPt(1) + maxPt(1)) / 2
  40. centPt(2) = (minPt(2) + maxPt(2)) / 2
  41. PseudoCenter = centPt
  42. End Function
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-30 13:59:59 | 显示全部楼层
从数学上讲,这是非常有问题的
第一反应是按照马特的建议使用质心,然而这失败了,因为;L“;由于质心在直线外,
路易斯&#039;s方法始终有效(如果我能正确阅读)
回复

使用道具 举报

170

主题

1424

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
2119
发表于 2007-5-30 14:19:53 | 显示全部楼层
很抱歉,我没有时间编写代码,但我会这样做:
我会从多段线生成一个区域。如果多段线不是闭合的,我会将plinetype的系统变量设置为2。然后发送命令pedit,可能会使用多个选项。然后将闭合多段线转换为;区域并获取质心。只要这些所谓的矩形足够接近一个,质心就应该很好
接下来,我将在该矩形中找到最长的线,并得到其起点和终点。从wcs获取其角度,方法是绘制一条线或分析多段线以获取最长的线及其长度。然后使用返回的质心和线/点的角度放置文本。
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-7 23:03 , Processed in 1.448401 second(s), 58 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表