|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
' s1 d: F) Q4 _; X内容:& e9 x4 R3 m# u g4 o. w `
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。
: K" i: w: r: O/ z1 j; e8 ^目的:" T: }/ }. l+ E: v) b
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。7 j' j1 H% v* T- v
NXOpen C++创建草图基本特征
0 e/ ^$ U. @& S7 c0 r- t- |' M
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍4 s$ h, ~; |& N& Y* ?
31 NXOpen C++创建基本特征-草图的创建(2)UI设计
. m' }4 a/ V0 I3 U8 [ 31 NXOpen C++创建基本特征-草图的创建(3)项目创建
5 M. z" M* M% v8 @8 N# w5 ?, g 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建
: [3 ~8 W8 K1 J$ N# |" b 31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图 G q2 Y5 m; T+ D
31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束; [# w1 L, ]# q& E& B$ \8 X9 j
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束' n6 U9 r/ d c( f2 x! Q
31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联! ^/ x& i/ J1 Z" t- v4 W. b
7 O5 i" n k) g: u. t4 |& m
* q0 z: S1 F4 t( w( {3 v! f
. ~5 l0 H) V: P5 k4 J" A
- {* u: ^" L% Z" r
; |. c0 \3 e2 U9 ]. p; V关键代码分享:8 V: S! `; ?+ B, E: P7 I, k; u
: m, |$ G0 H: U& n* U" M9 V0 i6 ^1 F+ B V, g' w& x
- ' v6 E& B2 C+ O# D
- void lesson31_createSkeTCh::createIPSketch()
" A/ f9 x* P& b1 r% C; U - {
6 r7 a& q8 G3 }" L* g5 {% y
. Q! E7 Q5 e' X4 l @8 E1 T; Z8 } h- Sketch *nullSketch(NULL);
/ C. j' q6 z3 b - SketchInPlaceBuilder *sketchInPlaceBuilder1;& d$ `0 @' R0 D- @: Y3 K& v0 P
- sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);
$ v, h' ?7 G7 f - sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);; ~! C r2 n7 _
- 4 t3 i# G$ K" E# `; j" n+ A1 ?
- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);, H Y1 ^: e' B% {/ n$ x
- 5 f3 @2 H+ L5 |) V0 g8 D
6 u9 P1 d) s4 Z3 t8 R- // add preference
* ? ^3 d* t5 {# f8 g' @1 g
* k0 O+ o4 w, q' m+ t4 e- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);
$ ^+ q) @; m2 `$ _9 j% P - # s8 S( ~2 O4 u/ _+ \2 X
- Drafting::PreferencesBuilder *preferencesBuilder1;& a) G; M$ l# Z+ q# e# X$ ]
- preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();& }: K2 g1 a0 q5 l4 L5 V5 @: e
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);
) C& {: Y- w& Q - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
* x$ ?1 D0 E9 { - preferencesBuilder1->Commit();1 k- D; Y5 ]$ W; @) A
- 5 T- Q% v: v/ |' h6 O
- preferencesBuilder1->Destroy();
& N8 x* Y* K8 q" [9 R' h! Y1 s3 N - 4 X4 z( L. W. p- A; D8 r
- 7 Z% g% n* b. L( ], Q
- NXObject *nXObject1;7 v& l/ h7 r+ _) |0 Z% ~
- nXObject1 = sketchInPlaceBuilder1->Commit();
* M$ z# y! |# u1 }6 x
3 R3 c X+ Q! g0 y. ?, [- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));: U8 d6 `- B. n6 Z
- Features::Feature *feature1;
! J# V. s+ j/ M3 W& `! y$ D1 U8 H - feature1 = sketch1->Feature();$ F6 j1 B. ~' \: I4 I
- feature1->SetName("XY-Plane-Sketch");
$ E2 e& h, n1 m Y! K) z5 c* | -
7 e3 h& d; g$ o- d- a u8 ^" M4 S - sketchInPlaceBuilder1->Destroy();! _2 @, P l2 A# M- h4 j) D
' o3 G% b7 c( V1 j2 B; z( H" e- //1 active sketch 5 s6 |' ]' W* f+ C* v1 T) k0 J6 V
) d) n' P( E3 j7 X& H- sketch1->Activate(Sketch::ViewReorientFalse);
- i* ~6 o! q8 p4 h, R2 V3 I -
) e& q. ]( ]" ~; [: M' c5 o - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");7 P- B+ A. z/ y
- double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");+ w1 f6 n/ _1 i
- double length = this->expressionLength->GetProperties()->GetDouble("Value");
$ M# L n3 g$ V; N8 [9 J' R - // 2.1 create geometry4 K [# H) k( ?
- ; s: G: U; k. P& r& A0 E/ h* q4 q7 k
- Point3d startPoint1(0.0, 0.0, 0.0);
& X4 U) y* g& R+ j" @2 A( U: D7 D - Point3d endPoint1(0, height1, 0.0);
1 J* b+ v2 _# o - Point3d endPoint2(length, height1, 0.0);
0 x* ?7 r2 C& S) g - Point3d endPoint3(length, height1-height2, 0.0);
( B: t# g% E$ Y8 x$ r6 O! t* E! T - Point3d origin = sketch1->Origin();
$ s# w5 K+ G# \" ]( h% n9 o - Point *originPoint = workPart->Points()->CreatePoint(origin);' @( a. N" m) ~! l- c, l a w1 h
* ]5 w6 n% K/ O9 o- Line *line1,*line2,*line3,*line4;/ b3 J5 e1 h( N
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);/ W7 q: N+ H, q: ]) e+ ^) w6 e& h
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);2 }# W( f! l* M$ j/ y
- line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);
. w9 Y! ^- o: n7 c9 ^2 ~; m! w4 S8 F - line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);2 ]+ ?. q0 `. Z: d- x9 ^$ v. Z- \
- // 2.2 add geometry0 z, X y$ m5 [" Y( u k# }
- % a6 N7 D8 d6 }2 e1 R) {7 D* u
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);" Q5 X3 m7 e/ n8 J, @+ r# L
- sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);
" }7 d' V3 \% z1 R: B - sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);7 H, m* Q3 X4 ?& \) K1 G
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);# G3 i, l* Z' X% X2 H1 L/ f# O2 Q, S
- : ^9 m, B& ]' M. L- f; a0 i
- //3 add constraints ( gemo)
/ l3 _7 H% r" ?: N - # v/ s: R; N( O5 ]% @- r
- Sketch::ConstraintGeometry geomline1;) Q* F( F- o1 U: |1 |
- geomline1.Geometry = line1;; _0 y1 K8 z0 T+ {& q3 ?" Y
- geomline1.PointType = Sketch::ConstraintPointTypeNone;
/ U" p7 X- T0 J4 I2 d- c- L5 g - geomline1.SplineDefiningPointIndex = 0;
5 `6 T; N/ `4 v - sketch1->CreateVerticalConstraint(geomline1);
- d1 n: Z0 l- O
1 }8 F, V- w& w4 x; I9 K- Sketch::ConstraintGeometry geomline2;
5 ]5 o* i: n5 j# Z- w - geomline2.Geometry = line2;
" B, ` Z/ D6 O( P! }8 ?0 g8 H - geomline2.PointType = Sketch::ConstraintPointTypeNone;
5 J8 c3 k0 E1 _% t0 e7 ` g - geomline2.SplineDefiningPointIndex = 0;
; {& Z% ^" b l - sketch1->CreateHorizontalConstraint(geomline2);
+ r4 i/ o+ T& {6 b' ^6 j - , N0 n1 z2 Q9 h0 Y4 I3 n+ M u
- Sketch::ConstraintGeometry geomline3;- g( x" o: y" R6 F0 ?
- geomline3.Geometry = line3;
7 T: K, w- V$ ?; Z( V7 s/ d - geomline3.PointType = Sketch::ConstraintPointTypeNone;; R* Y) |4 J9 ~1 f6 ~% V* ?( z
- geomline3.SplineDefiningPointIndex = 0;
: A- f0 p( W: }0 S$ o4 f - sketch1->CreateVerticalConstraint(geomline3);
( t+ W' z1 {& o - 4 ?! A, ~! H" P; [% q% }0 n( H" \
- Sketch::ConstraintGeometry geomline1_startPoint;' f8 i: u6 j, @ d
- geomline1_startPoint.Geometry = line1;
3 N, k* k. A% ?! F$ J! `( e* S - geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;
1 g/ y C# x' w$ ^5 W" ~7 k - geomline1.SplineDefiningPointIndex = 0;
1 r) Y. p: l1 d! K - Sketch::ConstraintGeometry geomline2_originPoint;
# A4 C0 u( J$ _3 U4 `" m* I - geomline2_originPoint.Geometry = originPoint;1 v) T- M" [% {7 _$ J
- geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
* R9 U9 z5 c" B7 q# f7 y - geomline2_originPoint.SplineDefiningPointIndex = 0;- B2 | `4 R8 A& u$ x: Z |
- sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);- [, D+ p6 O5 q4 Q5 y
- 5 f) A& E& L$ N/ U& k k Y
- //4 add constraints (dimension)
0 d- j0 M; b8 z+ l3 a9 k" @- r, R
& t# k. A& u5 b% N1 t `) o; W- NXObject *nullNXObject(NULL);
$ k! ? R- l4 u) a0 O% {* ]5 F
" @' {2 e& `. G8 q$ a z; E
7 w# u+ E! ~5 D; ~( s: k- stringstream s_height1,s_height2,s_length;. y8 o, \1 o, f3 v% W
- s_height1 <<height1;
# ^! t+ A' f5 v* k. |2 X" U- F2 Y+ D - s_height2 <<height2;
) O" I0 e5 o* U1 t1 W - s_length <<length;
0 C, T2 g5 o% k2 u6 m1 _, Y# a- M8 F
$ A) K; Y: f/ k( A- Expression *expression1,*expression2,*expression3;' n6 P6 z/ R7 O" a4 S
- expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
2 [, X; U/ H7 W9 l - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());1 [1 y) i5 C( T9 z, t) Q4 n
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());
+ |7 y. q2 H4 W/ @: |6 T ?
: V1 j# H+ B; c) y, W- // line1 dimension
, }3 c3 G, c( d8 P
0 B( q- D5 E8 f, W, v& F6 C8 h6 N- Sketch::DimensionGeometry dimLine1_startPoint;. J8 e% D7 u+ x4 ~, g
- dimLine1_startPoint.Geometry = line1;1 l7 I( h# z$ X
- dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;
8 H4 ?1 F# R! J! [. O - dimLine1_startPoint.AssocValue = 0;
: z3 Q$ ]" G1 \ - dimLine1_startPoint.HelpPoint.X = 0.0;
& J F; }0 r& E3 D' Y$ V - dimLine1_startPoint.HelpPoint.Y = 0.0;
3 Q( d, A5 d$ ]- T0 m9 @1 G - dimLine1_startPoint.HelpPoint.Z = 0.0;; Q' j: Z+ X& S0 f1 C+ z0 v
- dimLine1_startPoint.View = nullNXObject;* u' F: Z: l. J y. e5 H+ B
7 U h( P& Y* \+ {: m: k8 H& k- Sketch::DimensionGeometry dimLine1_endPoint;* a% A, h C% O; ~9 h3 a2 b$ h
- dimLine1_endPoint.Geometry = line1;
0 g T8 S. e2 {" E- W - dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;& \% t7 n2 _5 h; c p5 i
- dimLine1_endPoint.AssocValue = 0;
1 r" G$ y. `: E - dimLine1_endPoint.HelpPoint.X = 0.0;
7 k& D+ l1 c& T" O y/ y - dimLine1_endPoint.HelpPoint.Y = 0.0;
6 v S0 ?! u! L - dimLine1_endPoint.HelpPoint.Z = 0.0;
, B4 M& k \- w" {" a. S! `( |8 | - dimLine1_endPoint.View = nullNXObject;
! e( Z- |( E5 i
r7 {6 m* _9 F7 I/ u }6 ~; T' I- Point3d dimOrigin1(-100, height1/2, 0.0);5 a. X2 v" t% `) J" w( A
; y2 r5 Q3 R5 s& @! ~- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);& p: v% S# X/ |' l
- 3 i/ M2 e& T: ^ j7 U& O
- $ j2 K. h* m" G I
-
+ F& m/ T+ G) ^! | - // line2 dimension * ^. A4 ^; d' [
/ n q* n: ^; D8 M N8 h- Sketch::DimensionGeometry dimLine2_startPoint;
6 Q1 e; Q$ M/ o3 ^8 q$ z- } - dimLine2_startPoint.Geometry = line2;
7 U. m: D" ~* _1 S - dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;0 k. Z& G# B- t" @- I
- dimLine2_startPoint.AssocValue = 0;/ u+ l& T; v, q) R. n/ c) ~8 m
- dimLine2_startPoint.HelpPoint.X = 0.0;
% S/ t9 X; {, v, ~% p! M - dimLine2_startPoint.HelpPoint.Y = 0.0;
5 f/ w: o1 @' m0 D3 X3 f - dimLine2_startPoint.HelpPoint.Z = 0.0;3 y. n1 R+ C% t) _
- dimLine2_startPoint.View = nullNXObject;
7 K: ]5 n' I# i! c b - * m! q4 i3 N& a& Z4 [
- Sketch::DimensionGeometry dimLine2_endPoint;4 Y- k& f) q: S( x
- dimLine2_endPoint.Geometry = line2;
9 v! g% k% u1 }7 i9 o - dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;
/ ~$ c/ I# [5 |1 \/ O5 v; V! w - dimLine2_endPoint.AssocValue = 0;
8 }4 Z+ ?2 ]5 V) d - dimLine2_endPoint.HelpPoint.X = 0.0;
5 B. w) K9 J0 F, R4 a6 ^' H - dimLine2_endPoint.HelpPoint.Y = 0.0;
+ |# D) n; Z% \4 E0 E2 D" q$ y G - dimLine2_endPoint.HelpPoint.Z = 0.0;7 w$ v h; ?4 J' e( ^4 G2 f) K, G
- dimLine2_endPoint.View = nullNXObject;
$ a/ g! a9 K3 x6 i2 B1 P
~0 s; ] ]( q- Point3d dimOrigin2(length /2,100 + height1, 0.0);
+ p8 @7 X" ?- b0 P; N - % t) k( o7 j! J6 U: C) l8 B; O# m' S6 B& [
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);, ?# z$ Q2 ?7 L$ R7 d5 Z6 N0 J
- 0 q0 S& X B7 P
# k: F. C) b- Q# r2 O, e. V- % X! F( D/ c% [" f# X1 v
- // line3 dimension 8 }; M9 j& H$ P; I& ~* n
6 p# j, k- b# K- Sketch::DimensionGeometry dimLine3_startPoint;
) T& l2 J) Q6 `, e& j& J1 T7 |4 X - dimLine3_startPoint.Geometry = line3;: a* X8 D& g* y9 y9 Q
- dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;* m5 ^1 @( ~0 v R0 E
- dimLine3_startPoint.AssocValue = 0;
! O0 E( o4 X- n2 S; r# ^3 i4 ~ - dimLine3_startPoint.HelpPoint.X = 0.0;- P& q/ p! [2 N! F# ~
- dimLine3_startPoint.HelpPoint.Y = 0.0;1 ~! G( ]" t0 p$ u
- dimLine3_startPoint.HelpPoint.Z = 0.0;
5 a1 C0 M- f4 |' r/ e8 c - dimLine3_startPoint.View = nullNXObject;& X: n2 b: |6 _$ c
- % N' ?- I: e$ h7 q: C& o" ~
- Sketch::DimensionGeometry dimLine3_endPoint;+ l, Z0 T( H+ o
- dimLine3_endPoint.Geometry = line3;5 P# H% U$ g0 e* X/ B4 I6 C
- dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
1 ` P, v+ A+ H8 {! b; s( J' } - dimLine3_endPoint.AssocValue = 0;
{: y: }( b8 p4 M - dimLine3_endPoint.HelpPoint.X = 0.0;
8 Y( w L; D, a1 o/ B1 a - dimLine3_endPoint.HelpPoint.Y = 0.0;
, g: N1 C7 }0 n6 M: |# j - dimLine3_endPoint.HelpPoint.Z = 0.0;
( l1 `( l, K0 Q: u - dimLine3_endPoint.View = nullNXObject;
6 @2 _' r7 E3 U& h& v+ \$ q
: x/ S; _* M& O# B2 s$ w# M- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);1 P/ \# L- t5 O; ?6 f0 [
- ! ]; q0 ~8 Q6 u% o
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving); ^2 q) E7 r: ?( o8 X8 }4 R
- $ r' I' U" c/ a" J* k4 ^
- , i7 I3 a h' d
- + A' Y+ r& _, Y( | z) x4 ]
- //5 update & deactive
: D( {" h. N1 J+ z' e - sketch1->Update();
% c# h2 r8 P- Q- R3 e# b0 x8 [6 J - O6 h. ?- T0 X" S. ?& [
- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);6 Q& b! i$ d4 M# B) k1 G
, l+ s( ]! i3 n1 k( _8 U+ e9 a1 R) ?- delete expression1;( S$ E- ^" ~8 f4 ]9 M: s y s
- delete expression2;' o- F' t2 I+ K" j" ^
- delete expression3;! O) ^9 f( q; x" @& Y* o
-
1 S* F! p0 Q' R5 B$ P" D - }
5 M `0 s; o& A$ D
复制代码 @. {4 D2 P* t" `" a
7 C- @+ a) C: D/ y5 Q! T, H; `" ~ {. l" \ A. S
, l. P+ |: U. h* _7 q' n1 L( q
|
|