当前位置:网站首页>2021-07-05c /cad secondary development create arc (4)
2021-07-05c /cad secondary development create arc (4)
2022-07-02 06:59:00 【Xu Jirong】
The code is as follows :
ArcExam.cs
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace _2 Graphic Creation
{
public class ArcExam
{
[CommandMethod("ArcDemo")]
public void ArcDemo()
{
Arc arc1 = new Arc();
arc1.Center = new Point3d(0, 0, 0);
arc1.StartAngle = 0;
arc1.EndAngle = Math.PI;
arc1.Radius = 100;
double startDegree = 45;
Arc arc2 = new Arc(new Point3d(500, 500, 0), 20,BaseTool.DegreeToAngle(startDegree), Math.PI);
Arc arc3 = new Arc(new Point3d(100, 100, 0), new Vector3d(0, 0, 1), 20, Math.PI / 4, Math.PI / 2);
Database db = HostApplicationServices.WorkingDatabase;
AddEnityTool.AddEnityToModelSpace(db, arc1,arc2,arc3);
}
}
}
Code parsing :
The above code is basically for introduction CAD The usage of the inner arc method
Arc arc1 = new Arc();
arc1.Center = new Point3d(0, 0, 0);
arc1.StartAngle = 0;
arc1.EndAngle = Math.PI;
arc1.Radius = 100;
Center The coordinates of the center of the circle ;StartAngle Start the arc ;EndAngle End arc ;Radius radius ;
Note that the attribute here is radian
double startDegree = 45;
Arc arc2 = new Arc(new Point3d(500, 500, 0), 20,BaseTool.DegreeToAngle(startDegree), Math.PI);
Here is also the radian value , We use the angle to radian method created previously to 45 Degrees to radians
From left to right , Followed by the center of the circle 、 radius 、 Start the arc 、 End arc .
arc3 involve Vector3d, I draw a two-dimensional picture , In less than , It refers to Cartesian coordinates .
Draw an arc at three points
The code is as follows :
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
namespace _2 Graphic Creation
{
public class ArcExam
{
[CommandMethod("ArcDemo")]
public void ArcDemo()
{
Database db = HostApplicationServices.WorkingDatabase;
Point3d startPoint = new Point3d(100, 100, 0);
Point3d endPoint = new Point3d(200, 200, 0);
Point3d pointOnArc = new Point3d(150, 100, 0);
CircularArc3d cArc = new CircularArc3d(startPoint,pointOnArc,endPoint);
double radius = cArc.Radius;
Point3d center = cArc.Center;
Vector3d cs = center.GetVectorTo(startPoint);
Vector3d ce = center.GetVectorTo(endPoint);
Vector3d xvector = new Vector3d(1, 0, 0);
double startAngle = cs.Y > 0 ? xvector.GetAngleTo(cs) : -xvector.GetAngleTo(cs);
double endAngle = ce.Y > 0 ? xvector.GetAngleTo(ce) : -xvector.GetAngleTo(ce);
Arc arc = new Arc(center,radius,startAngle,endAngle);
AddEnityTool.AddEnityToModelSpace(db, arc);
}
}
}
Code interpretation :
Database db = HostApplicationServices.WorkingDatabase;
Point3d startPoint = new Point3d(100, 100, 0);
Point3d endPoint = new Point3d(200, 200, 0);
Point3d pointOnArc = new Point3d(150, 100, 0);
CircularArc3d cArc = new CircularArc3d(startPoint,pointOnArc,endPoint);
Three points are created , And then through CircularArc3d Method creates an arc object
Try to use ARC Method to realize three-point circle
double radius = cArc.Radius;
Point3d center = cArc.Center;
First, obtain the radius and center point of the upper arc .
Vector3d cs = center.GetVectorTo(startPoint);
Vector3d ce = center.GetVectorTo(endPoint);
The code here can be passed GetVectorTo Methods the vectors from the center of the circle to two points are obtained respectively .
Vector3d xvector = new Vector3d(1, 0, 0);
Create a X The unit vector of the direction
double startAngle = cs.Y > 0 ? xvector.GetAngleTo(cs) : -xvector.GetAngleTo(cs);
double endAngle = ce.Y > 0 ? xvector.GetAngleTo(ce) : -xvector.GetAngleTo(ce);
adopt GetAngleTo Get the angle between two vectors ( Radian value ), because GetAngleTo The included angle obtained ( This is the radian system ) Are positive , Take its negative value through judgment , You can draw this arc
Pictured above , Red thread 、 Green line and X The included angles of the shafts are 42°,GetAngleTo The values obtained are 42° The radian value of ( Comes at a time ), So in order to distinguish , Make a judgment ,startPoint or endPoint Of Y When the coordinate is negative, a negative radian value is returned .
Encapsulate the code page that created the arc above into AddEnityTool.cs in
First, judge X The included angle of the shaft is X The code above or below the axis is encapsulated in BaseTool.cs in
/// <summary>
/// Get two points in a straight line to X The Angle between the axes ( There are positive and negative )
/// </summary>
/// <param name="startPoint"> The starting point </param>
/// <param name="endPoint"> End </param>
/// <returns></returns>
public static double GetAngleToXaxis(Point3d startPoint, Point3d endPoint)
{
// Declare a relationship with X A vector with parallel axes
Vector3d temp = new Vector3d(1, 0, 0);
// Get the vector from start to end
Vector3d VsToe = startPoint.GetVectorTo(endPoint);
return VsToe.Y > 0 ? temp.GetAngleTo(VsToe) : -temp.GetAngleTo(VsToe);
}
Encapsulated in the GetAngleToXaxis Method
The three points given are not prevented from being on the same straight line , A judgment condition is also added to BaseTool.cs in
/// <summary>
/// Judge whether the three points are on the same straight line
/// </summary>
/// <param name="firstPoint"> The first point </param>
/// <param name="secondPoint"> Second points </param>
/// <param name="thirdPoint"> The third point </param>
/// <returns></returns>
public static bool IsOnOneLine(Point3d firstPoint, Point3d secondPoint, Point3d thirdPoint)
{
Vector3d v21 = secondPoint.GetVectorTo(firstPoint);
Vector3d v23 = secondPoint.GetVectorTo(thirdPoint);
if (v21.GetAngleTo(v23) == 0 || v21.GetAngleTo(v23) == Math.PI)
{
return true;
}
else
{
return false;
}
}
Encapsulated in the IsOnOneLine Method
AddEnityTool.cs In the code
/// <summary>
/// Create an arc through three points
/// </summary>
/// <param name="db"> Graphic database </param>
/// <param name="startPoint"> The starting point </param>
/// <param name="pointOnArc"> Point on arc </param>
/// <param name="endPoint"> End </param>
/// <returns></returns>
public static object AddArcToModelSpace(Database db, Point3d startPoint, Point3d pointOnArc, Point3d endPoint)
{
// First judge whether the three points are on the same straight line
if (BaseTool.IsOnOneLine(startPoint, pointOnArc, endPoint))
{
return ObjectId.Null;
}
CircularArc3d cArc = new CircularArc3d(startPoint, pointOnArc, endPoint);
Arc arc = new Arc(cArc.Center, cArc.Radius, BaseTool.GetAngleToXaxis(cArc.Center,startPoint), BaseTool.GetAngleToXaxis(cArc.Center,endPoint));
// Add graphics database
return AddEnityTool.AddEnityToModelSpace(db,arc);
}
Through the center of the circle 、 The starting point 、 Draw a circle with included angles (AddEnityTool.cs)
/// <summary>
/// Through the center 、 The starting point 、 Draw a circle with included angles
/// </summary>
/// <param name="db"> Graphic database </param>
/// <param name="center"> center of a circle </param>
/// <param name="startPoint"> The starting point </param>
/// <param name="degree"> Angle </param>
/// <returns>ObjectId</returns>
public static ObjectId AddArcToModelSpace(Database db, Point3d center, Point3d startPoint, double degree)
{
// Get the radius
double radius = BaseTool.GetDistanceBetweenTwoPoint(center, startPoint);
// Get the starting angle
double startAngle = BaseTool.GetAngleToXaxis(center, startPoint);
// Declare an arc object
Arc arc = new Arc(center, radius, startAngle, startAngle + BaseTool.DegreeToAngle(degree));
return AddEnityTool.AddEnityToModelSpace(db, arc);
}
The code to obtain the radius is as follows (BaseTool.cs)
/// <summary>
/// Get the distance between two points
/// </summary>
/// <param name="point1"></param>
/// <param name="point2"></param>
/// <returns></returns>
public static double GetDistanceBetweenTwoPoint(Point3d point1, Point3d point2)
{
return Math.Sqrt((point1.X - point2.X) * (point1.X - point2.X) + (point1.Y - point2.Y) * (point1.Y - point2.Y) + (point1.Z - point2.Z) * (point1.Z - point2.Z));
}
边栏推荐
- Latex compilation error I found no \bibstyle &\bibdata &\citation command
- js中对于返回Promise对象的语句如何try catch
- Utilisation de la carte et de foreach dans JS
- ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
- The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
- Build learning tensorflow
- js创建一个自定义json数组
- Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
- (the 100th blog) written at the end of the second year of doctor's degree -20200818
- [leetcode question brushing day 35] 1060 Missing element in ordered array, 1901 Find the peak element, 1380 Lucky number in matrix
猜你喜欢
Flex Jiugongge layout
[Zhang San learns C language] - deeply understand data storage
20201002 vs 2019 qt5.14 developed program packaging
Sqli labs customs clearance summary-page4
Stack (linear structure)
Self study table Au
Review of reflection topics
在php的开发环境中如何调取WebService?
sqli-labs通关汇总-page1
Apt command reports certificate error certificate verification failed: the certificate is not trusted
随机推荐
默认google浏览器打不开链接(点击超链接没有反应)
Queue (linear structure)
SQL注入闭合判断
UEditor . Net version arbitrary file upload vulnerability recurrence
Fe - use of weex development weex UI components and configuration use
Virtualenv and pipenv installation
SQL injection closure judgment
Kotlin - verify whether the time format is yyyy MM DD hh:mm:ss
SQLI-LABS通关(less15-less17)
Solve the problem of bindchange event jitter of swiper component of wechat applet
SQLI-LABS通关(less1)
2020-9-23 use of QT timer qtimer class.
js中map和forEach的用法
Record RDS troubleshooting once -- RDS capacity increases dramatically
js判断数组中对象是否存在某个值
Usage of map and foreach in JS
2021-07-05C#/CAD二次开发创建圆弧(4)
js的防抖和节流
MySQL index
Code execution sequence with and without resolve in promise