CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

计算点和值 (.NET)

2023-1-1 15:42| 发布者: admin| 查看: 320| 评论: 0|来自: AutoCAD

通过使用编辑器对象以及“几何”和“运行时”命名空间提供的方法,可以快速解决数学问题或定位图形中的点。一些可用的方法是:

  • 使用 and 方法获取两个 2D 或 3D 点之间的距离GetDistanceToDistanceTo
  • 使用两个 2D 点从 X 轴获取角度,使用具有返回值属性的方法GetVectorToAngle
  • 使用该方法将角度作为字符串转换为实数(双精度)值StringToAngle
  • 使用该方法将角度从实数(双精度)值转换为字符串AngleToString
  • 使用该方法将字符串的距离转换为实数(双精度)值StringToDistance
  • 用该方法查找用户输入的两个点之间的距离GetDistance
注意:AutoCAD .NET API 不包含基于距离和角度(极点)计算点以及在不同坐标系之间转换坐标的方法。如果需要这些实用工具,则需要使用 ActiveX 自动化库中的 and方法。PolarPointTranslateCoordinates

从 X 轴获取角度

本示例计算两点之间的向量,并确定与 X 轴的角度。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("AngleFromXAxis")> _
Public Sub AngleFromXAxis()
  Dim pt1 As Point2d = New Point2d(2, 5)
  Dim pt2 As Point2d = New Point2d(5, 2)
 
  Application.ShowAlertDialog("Angle from XAxis: " & _
                              pt1.GetVectorTo(pt2).Angle.ToString())
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("AngleFromXAxis")]
public static void AngleFromXAxis()
{
  Point2d pt1 = new Point2d(2, 5);
  Point2d pt2 = new Point2d(5, 2);
 
  Application.ShowAlertDialog("Angle from XAxis: " +
                              pt1.GetVectorTo(pt2).Angle.ToString());
}

VBA/ActiveX 代码参考

Sub AngleFromXAxis()
    ' This example finds the angle, in radians, between the X axis
    ' and a line defined by two points.
 
    Dim pt1(0 To 2) As Double
    Dim pt2(0 To 2) As Double
    Dim retAngle As Double
 
    pt1(0) = 2: pt1(1) = 5: pt1(2) = 0
    pt2(0) = 5: pt2(1) = 2: pt2(2) = 0
 
    ' Return the angle
    retAngle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
 
    ' Display the angle found
    MsgBox "The angle in radians between the X axis is " & retAngle
End Sub

计算极点

此示例根据基点、角度和距离计算点。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
 
Public Shared Function PolarPoints(ByVal pPt As Point2d, _
                                   ByVal dAng As Double, _
                                   ByVal dDist As Double)
 
  Return New Point2d(pPt.X + dDist * Math.Cos(dAng), _
                     pPt.Y + dDist * Math.Sin(dAng))
End Function
 
Public Shared Function PolarPoints(ByVal pPt As Point3d, _
                                   ByVal dAng As Double, _
                                   ByVal dDist As Double)
 
  Return New Point3d(pPt.X + dDist * Math.Cos(dAng), _
                     pPt.Y + dDist * Math.Sin(dAng), _
                     pPt.Z)
End Function
 
<CommandMethod("PolarPoints")> _
Public Sub PolarPoints()
  Dim pt1 As Point2d
  pt1 = PolarPoints(New Point2d(5, 2), 0.785398, 12)
 
  Application.ShowAlertDialog(vbLf & "PolarPoint: " & _
                              vbLf & "X = " & pt1.X & _
                              vbLf & "Y = " & pt1.Y)
 
  Dim pt2 As Point3d
  pt2 = PolarPoints(New Point3d(5, 2, 0), 0.785398, 12)
 
  Application.ShowAlertDialog(vbLf & "PolarPoint: " & _
                              vbLf & "X = " & pt2.X & _
                              vbLf & "Y = " & pt2.Y & _
                              vbLf & "Z = " & pt2.Z)
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
 
static Point2d PolarPoints(Point2d pPt, double dAng, double dDist)
{
  return new Point2d(pPt.X + dDist * Math.Cos(dAng),
                     pPt.Y + dDist * Math.Sin(dAng));
}
 
static Point3d PolarPoints(Point3d pPt, double dAng, double dDist)
{
  return new Point3d(pPt.X + dDist * Math.Cos(dAng),
                     pPt.Y + dDist * Math.Sin(dAng),
                     pPt.Z);
}
 
[CommandMethod("PolarPoints")]
public static void PolarPoints()
{
  Point2d pt1 = PolarPoints(new Point2d(5, 2), 0.785398, 12);
 
  Application.ShowAlertDialog("\nPolarPoint: " +
                              "\nX = " + pt1.X +
                              "\nY = " + pt1.Y);
 
  Point3d pt2 = PolarPoints(new Point3d(5, 2, 0), 0.785398, 12);
 
  Application.ShowAlertDialog("\nPolarPoint: " +
                              "\nX = " + pt2.X +
                              "\nY = " + pt2.Y +
                              "\nZ = " + pt2.Z);
}

VBA/ActiveX Code Reference

Sub PolarPoints()
    ' This example finds the coordinate of a point that is a given
    ' distance and angle from a base point.
 
    Dim polarPnt As Variant
    Dim basePnt(0 To 2) As Double
    Dim angle As Double
    Dim distance As Double
 
    basePnt(0) = 2#: basePnt(1) = 2#: basePnt(2) = 0#
    angle = 0.785398
    distance = 6
    polarPnt = ThisDrawing.Utility.PolarPoint(basePnt, angle, distance)
 
    MsgBox vbLf + "PolarPoint: " + _
           vbLf + "X = " + CStr(polarPnt(0)) + _
           vbLf + "Y = " + CStr(polarPnt(1)) + _
           vbLf + "Z = " + CStr(polarPnt(2))
End Sub

Find the distance between two points with the GetDistance method

This example uses the method to obtain two points and displays the calculated distance. GetDistance

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("GetDistanceBetweenTwoPoints")> _
Public Sub GetDistanceBetweenTwoPoints()
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
 
  Dim pDblRes As PromptDoubleResult
  pDblRes = acDoc.Editor.GetDistance(vbLf & "Pick two points: ")
 
  Application.ShowAlertDialog(vbLf & "Distance between points: " & _
                              pDblRes.Value.ToString())
End Sub

C#

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("GetDistanceBetweenTwoPoints")]
public static void GetDistanceBetweenTwoPoints()
{
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
 
  PromptDoubleResult pDblRes;
  pDblRes = acDoc.Editor.GetDistance("\nPick two points: ");
 
  Application.ShowAlertDialog("\nDistance between points: " +
                              pDblRes.Value.ToString());
}

VBA/ActiveX 代码参考

Sub GetDistanceBetweenTwoPoints()
    Dim returnDist As Double
 
    ' Return the value entered by user. A prompt is provided.
    returnDist = ThisDrawing.Utility.GetDistance(, "Pick two points.")
 
    MsgBox "The distance between the two points is: " & returnDist
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 14:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部