今天有个同学请教为啥自己写的渐开线方程不能画出草图来,随之进行了一番研究。。。
2D explicit curve:
A parabola y = x^2
CreateEquationSpline2("", “x^2”, “”, “-5”, “5”, False, 0, 0, 0, True, True)
2D parametric curve:
A spiral x = tsin(t); y = tcos(t)
CreateEquationSpline2(“tsin(t)", "tcos(t)”, “”, “0”, “4*pi”, False, 0, 0, 0, True, True)
3D parametric curve:
A helix x = sin(t); y = cos(t); z = t/5
CreateEquationSpline2("sin(t), “cos(t)”, “t/5”, “0”, “30”, False, 0, 0, 0, True, True)
函数参数是需要转换为字符串的不接受Double的参数 ,参数中的“t”是特定的值。
大部分同学写的参数化不能变化都是这里写的不对导致的!!
例如 渐开线半径 double r=50;
如果参数的公式为 rcos(t)+rt*sin(t),则需要写成r.ToString()+"*cos(t)+"+r.ToString()+“tsin(t)” 的样子
*
C#语言的写法
ModelDoc2 modelDoc2 = null;
ISldWorks swApp =Class1.ConnectToSolidWorks();
double r =Convert.ToDouble( Banjing.Text);
double StartAngle =Convert.ToDouble( QiShiJiaoDu.Text)*(Math.PI/180);
double EndAngle =Convert.ToDouble( ZhongZhiJiaoDu.Text) * (Math.PI / 180);
this.Hide();
if (swApp != null)
{
try
{
modelDoc2 = (ModelDoc2)swApp.ActiveDoc; //当前零件
modelDoc2.Extension.SelectByID2("前视基准面","PLANE",0,0,0,false,0,null,0);
SketchManager sketchManager= modelDoc2.SketchManager;
swApp.SetUserPreferenceToggle(10,false);
sketchManager.InsertSketch(true);
SketchSegment skSegment = default(SketchSegment);
skSegment = (SketchSegment)modelDoc2.SketchManager.CreateEquationSpline2
(r.ToString()+"*cos(t)+"+r.ToString()+"*t*sin(t)", r.ToString() + "*sin(t)-"+ r.ToString() + "*t*cos(t)", "", StartAngle.ToString(), EndAngle.ToString(), false, 0, 0, 0, true,true);
sketchManager.InsertSketch(true);
this.Show();
}
catch (Exception e2)
{
MessageBox.Show(e2.Message);
}
Vb语言的写法
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Const pi! = 3.141592628
Private Sub CommandButton1_Click()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Dim r As String
Dim qishi As Double
Dim zongzhi As Double
r = banjing.Text
qishi = CDbl(qis.Text) * pi / 180
zongzhi = CDbl(zongz.Text) * pi / 180
boolstatus = Part.Extension.SelectByID2("前视基准面", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
Dim equationDriveCurve As Object
Set equationDriveCurve = Part.SketchManager.CreateEquationSpline2(r & "*cos(t)+" & r & "*t*sin(t)", r & "*sin(t)-" & r & "*t*cos(t)", "", CStr(qishi), CStr(zongzhi), False, 0, 0, 0, True, True)
Part.ClearSelection2 True
Part.SketchManager.InsertSketch True