|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
9 S# } k, w% ^6 l& i) u: s+ m% E内容:. G( h* G1 |2 [1 N
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。
( }' _3 ^% i; S! ], ] i9 T' n7 W目的:
$ U1 Y2 t Z- d) n在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
+ e& F! j3 e4 p i( I) X( W
NXOpen C++创建草图基本特征
4 I7 J+ `7 y) c 31 NXOpen C++创建基本特征-草图的创建(1)项目介绍
8 A; }" l F: S- w! e. k 31 NXOpen C++创建基本特征-草图的创建(2)UI设计
( S- [" Y H* k. v8 e6 j9 c5 x$ f 31 NXOpen C++创建基本特征-草图的创建(3)项目创建
) x: N7 b8 \+ O* h; Z 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建
$ v: v4 r/ }) \' y5 \2 q 31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图* Y- N- W$ s/ K2 s: A
31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束
: y8 o: d' R, ~0 F: e3 q 31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束
4 P% d% m2 G. }0 \4 D2 A, Y! @. p 31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联9 d# D" ]0 Y4 @# T& b' b9 C
# i) v; b1 U7 c V) b
) T% h0 O! l, l9 s0 z& E8 w! p* N l! ^5 z
# n( R* D X/ \
. ~6 Y) Z3 A: w关键代码分享:" f# [* P" M6 L3 ~- _
: Y" J9 k8 v5 E, k) c Q+ n
4 C& v3 M" Y& C: D9 a- 7 ~) d/ A' |& a- A
- void lesson31_createSkeTCh::createIPSketch()
3 T" M( b/ z4 w' ^) @# `7 y - {9 a$ q% f6 o% m& j) o
; \2 }6 t# C' d# v- Sketch *nullSketch(NULL);% N) U9 `" N- m- o, i+ Y
- SketchInPlaceBuilder *sketchInPlaceBuilder1;
+ y; U- ~; z! n/ ` - sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);
4 V% [: M/ ^9 h1 i6 ]& R$ p - sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);) F- n0 w( o6 B
- S+ R: Y ?! Y$ m3 G2 ?- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);8 Q8 n/ l3 Q, K, V+ }3 P+ E
& O1 x. B: s) H7 H, e! O
* v2 P. }6 T" H, m+ x- // add preference
# }7 g& i) a8 Y- j; k+ e
9 n6 C; n8 O( A, @6 z5 ~' C- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);
. W/ K0 t8 o5 M1 F* x0 T - # }; ]! ^6 }4 Q& m# D
- Drafting::PreferencesBuilder *preferencesBuilder1;
7 z5 I' o6 u! g+ M+ X - preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();& V }/ r P4 Q T6 f9 I
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);& ^) X. {4 X" U( b2 F8 \" i' A
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);4 q. O' ?( _3 ]$ L9 G3 H
- preferencesBuilder1->Commit();
) y5 L2 D3 D; Z7 [( H' f, U
, i! {3 S0 Y; @* W- preferencesBuilder1->Destroy();
$ q3 A0 L, i: h9 b" e* U l
# x" n2 M# I4 f
. p& [! W; [: U" M: ~5 Y- NXObject *nXObject1;$ ]! J& W5 h! H7 K
- nXObject1 = sketchInPlaceBuilder1->Commit();% @& t" K) ?4 Y/ S {5 Q5 T9 Q
8 X, p+ @! W. j- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));, ?9 g1 T6 R* M
- Features::Feature *feature1;) ? W) i' h6 n: g# j9 A
- feature1 = sketch1->Feature();1 n; Z& d7 G* w+ ~+ e' B
- feature1->SetName("XY-Plane-Sketch");0 G r. B5 g/ D- t7 ]
- 4 N9 D- x. P0 y+ l/ G, B/ y
- sketchInPlaceBuilder1->Destroy();' r _0 Q" ^& M9 W3 H
/ ]+ ^1 L- _& C7 O+ m, c- //1 active sketch ' Y/ Y: S% m3 S. X
- " D2 `- T) N4 W
- sketch1->Activate(Sketch::ViewReorientFalse);
' u A0 ]# x9 m$ U! `4 s6 j -
. o9 b$ D( e/ L; G+ T8 D8 G - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
% l- z" D) Y7 R/ Y6 [% c# m/ X( h - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");
! I% i" |" @0 z - double length = this->expressionLength->GetProperties()->GetDouble("Value");- B7 R* x m8 O# q
- // 2.1 create geometry
) E& N- o2 T% b' `
, |* u, r. J, u; d$ e- @ k- Point3d startPoint1(0.0, 0.0, 0.0);- h2 ?5 P3 V7 G& w+ p" f
- Point3d endPoint1(0, height1, 0.0);
) k+ W2 n3 r+ z* d+ b5 ]+ t9 i - Point3d endPoint2(length, height1, 0.0);
/ }3 R0 {' ^- n4 m- ^8 y - Point3d endPoint3(length, height1-height2, 0.0);' N+ J* `9 P& w7 A+ y! r4 K# }
- Point3d origin = sketch1->Origin();
8 b6 t5 U+ C- d+ {0 ?6 w - Point *originPoint = workPart->Points()->CreatePoint(origin);
0 T, z* B; ?7 e2 J, A$ N
0 E2 ?& i5 M" I' U' _3 W$ b1 w- Line *line1,*line2,*line3,*line4; F) ~1 D: X: Q; O# B) l- w
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);9 T& V0 d& p& [8 E5 { Z6 \
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);
8 Z5 q$ B7 d. {3 n$ W v" t% L - line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);, W8 W6 { n1 s( y8 y, Q
- line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
% e+ f. ^; F, i - // 2.2 add geometry7 p+ M, a! ~2 D8 }3 \
8 {/ Y9 K5 p0 ]- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
+ u3 E# l# I- Q+ A( w/ g8 U - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);
3 T Q$ z, l9 ~( Y T) [ - sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);
5 v8 V+ b2 n" G& e - sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);' j! q. l$ o3 R: _' f9 |
- ' X6 I! l- W6 O5 z# H1 l- J
- //3 add constraints ( gemo)! x d6 a. T* O8 `0 J) ^2 ?2 E+ v
- 2 k: r6 V/ |7 e% @( o
- Sketch::ConstraintGeometry geomline1;
4 {7 J% B$ S& A: [3 q - geomline1.Geometry = line1;, t9 P9 K! a+ H! k$ k- P }
- geomline1.PointType = Sketch::ConstraintPointTypeNone;
' U- F! g# [. r) @ - geomline1.SplineDefiningPointIndex = 0;
0 b+ t& w' {4 x9 i- K; D - sketch1->CreateVerticalConstraint(geomline1);
5 t+ |% z( d& K4 { - ' @0 D, E' b2 w
- Sketch::ConstraintGeometry geomline2;
% O, w4 G7 J, K: `* y- b9 \ - geomline2.Geometry = line2;
2 t8 q$ U8 L$ h - geomline2.PointType = Sketch::ConstraintPointTypeNone;- f/ [. l7 B) O
- geomline2.SplineDefiningPointIndex = 0;
" Q/ q; E8 q. n0 E4 v! |4 S - sketch1->CreateHorizontalConstraint(geomline2); \! l2 ~/ b8 I& X2 O
% J, N( O( ~! @, ?/ ^6 Z- Sketch::ConstraintGeometry geomline3;/ j9 c- z! s* o6 G0 `, b6 \
- geomline3.Geometry = line3;; }; Y3 M& b! [& o" ^' |
- geomline3.PointType = Sketch::ConstraintPointTypeNone;
: L) x) r1 y+ v0 s6 B" J - geomline3.SplineDefiningPointIndex = 0;! z9 P0 O6 B' j( o) K5 X
- sketch1->CreateVerticalConstraint(geomline3);! |% Y1 C/ F. w) \; r. o
- ( s5 T4 R6 I: ^& i' h# R7 I$ N9 j
- Sketch::ConstraintGeometry geomline1_startPoint;
n& @/ H$ x* H+ w8 c- V - geomline1_startPoint.Geometry = line1;
) a- k7 e) v+ Y' l - geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;
7 Z: Z' y8 e* o( h8 \! }0 D$ i; y% Y - geomline1.SplineDefiningPointIndex = 0;% {4 h+ |7 _6 `8 i1 a
- Sketch::ConstraintGeometry geomline2_originPoint;/ v; `% p( C$ g9 N
- geomline2_originPoint.Geometry = originPoint;
# ]" w/ ]; Z7 d+ c1 e0 Z - geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
& _3 q' \3 p; _ - geomline2_originPoint.SplineDefiningPointIndex = 0;
/ |& G( J0 Z# B" A - sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);
4 v" c( W, x1 w6 L3 G" V
% ^ e) i2 [% w- ?' M% p; ~- //4 add constraints (dimension)) p( p/ H0 U8 }7 s* d" p
7 W& V; T7 h, @) v: {- v2 o m+ s- NXObject *nullNXObject(NULL);
$ k0 |$ E6 Q- J7 T0 ~6 x - ' y6 l: \6 |# Z- h/ I+ D8 h
- ) R8 f! n( V3 i5 {+ ^
- stringstream s_height1,s_height2,s_length;
* V5 q$ U0 J; }; c' X9 V) q - s_height1 <<height1;
$ b$ }: H% H/ y' n - s_height2 <<height2;1 ]; I# w" E9 @3 Q$ c
- s_length <<length;2 f% v( E0 |/ |: b9 e, h( x
- 1 U' U$ F3 n! g0 A) ]. ]+ p/ P# g
- Expression *expression1,*expression2,*expression3;
' v; S! E" G$ t3 a - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str()); I' ]4 Z# H: Y- o7 w
- expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());
2 S$ Q' L' a% `! ]' W: o1 H - expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());7 |4 e K" \! _$ j: Y
& D! @; U+ l) K. U- j3 z7 G; L, ?* @5 u- // line1 dimension % o9 H9 ?* h2 h
. v) u7 U/ n/ k6 U0 C- Sketch::DimensionGeometry dimLine1_startPoint;
6 L+ e$ ?- A2 s4 B* x! L; ] - dimLine1_startPoint.Geometry = line1;
; w1 `. I" n) t- b {- t - dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;) ]+ w7 m! s# u; Q# C, D
- dimLine1_startPoint.AssocValue = 0; e r- ^ R7 m9 s
- dimLine1_startPoint.HelpPoint.X = 0.0;% D: j* K6 i9 |: y( Q
- dimLine1_startPoint.HelpPoint.Y = 0.0;
, o2 [7 H. c: B1 e6 { - dimLine1_startPoint.HelpPoint.Z = 0.0;
7 D* Q* E- p& W- d W* c: F$ Y8 S - dimLine1_startPoint.View = nullNXObject;7 }3 n* b r2 n& r
- % G% F; d$ l7 E6 K
- Sketch::DimensionGeometry dimLine1_endPoint;, W# |1 {/ P7 \; h3 m' }! H4 Y! q
- dimLine1_endPoint.Geometry = line1;
4 W# [% x' T6 K+ y - dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;* |8 E" H2 L; p- v
- dimLine1_endPoint.AssocValue = 0;$ Y; L4 [ B E& ?9 a
- dimLine1_endPoint.HelpPoint.X = 0.0;$ y* V6 c) N6 S6 |9 c' W
- dimLine1_endPoint.HelpPoint.Y = 0.0;
# l& V8 }. z2 y8 `% y1 V - dimLine1_endPoint.HelpPoint.Z = 0.0;+ R9 Y! z: y0 d7 Q# v
- dimLine1_endPoint.View = nullNXObject;' z. C' p1 y4 h( u& C
- : I. q7 b. t$ o0 {
- Point3d dimOrigin1(-100, height1/2, 0.0);7 m: _; b( X, @) _0 Z y' m
- - [( y3 I- u0 h! g+ s) C0 B# X
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
P2 I; z! z# Q6 U - / b, y, ]) a% \0 k' P, g
! i0 @+ E: H/ C; h7 U-
% Z. w0 {5 g" I: ^2 u - // line2 dimension
+ O& m3 W; x7 O - $ X0 y( P" r$ K& A8 m, r* P
- Sketch::DimensionGeometry dimLine2_startPoint; L; d _/ ?( T X
- dimLine2_startPoint.Geometry = line2;3 ~! m B* R2 p. |& H- T J r
- dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;% T: B( _! M, L6 k
- dimLine2_startPoint.AssocValue = 0;% Y% v0 R" C3 V% V/ k5 [# y* ^
- dimLine2_startPoint.HelpPoint.X = 0.0;1 |7 R* P2 a' m: f z
- dimLine2_startPoint.HelpPoint.Y = 0.0;
7 p: {; L2 k- v* @ - dimLine2_startPoint.HelpPoint.Z = 0.0;
* H+ h3 A% ?. Z8 d - dimLine2_startPoint.View = nullNXObject;+ m/ L: o$ s' k
1 y4 z6 @7 A/ \4 h4 k5 A5 p- Sketch::DimensionGeometry dimLine2_endPoint;; A2 \/ i5 ?9 Y' z
- dimLine2_endPoint.Geometry = line2;5 K5 ]- N2 C G a" t
- dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;
4 l% e, ]( K) W8 n1 j2 k - dimLine2_endPoint.AssocValue = 0;
3 o: m5 E* |6 I* O* d - dimLine2_endPoint.HelpPoint.X = 0.0;
: `8 A7 E# ]% o6 j - dimLine2_endPoint.HelpPoint.Y = 0.0;
1 F( \9 m* k' S* t6 E* p+ T - dimLine2_endPoint.HelpPoint.Z = 0.0;
1 L2 k7 e7 @' f - dimLine2_endPoint.View = nullNXObject;
* w, @8 J* q3 O; K/ C/ A - 8 R( S" b7 i% z+ {) L" k2 M) ]
- Point3d dimOrigin2(length /2,100 + height1, 0.0);
, O/ d, o% [! g - # {0 j6 g6 e5 \( j. m# t! V
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);0 i, C9 k2 ^0 [$ w7 W0 j5 ]
+ l1 T, V- j& h- 5 x" J# @6 ^, Y: G+ m3 g& {
- 6 x2 i. u8 F- T+ m$ X* a5 l
- // line3 dimension 4 E- P" R4 F$ f- q* S
; ^; B0 ?9 U# v+ w: m4 I- Sketch::DimensionGeometry dimLine3_startPoint;
+ z, D. L: k8 B# `- h - dimLine3_startPoint.Geometry = line3;
4 ? o- \* Z* Y- ~: O - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;0 E6 { P9 m0 j- Q0 b+ J. ~) @
- dimLine3_startPoint.AssocValue = 0;; j" i2 [+ m O+ H
- dimLine3_startPoint.HelpPoint.X = 0.0;
( C' d, `3 n$ d9 ^3 F5 P8 R. w* c - dimLine3_startPoint.HelpPoint.Y = 0.0;0 U$ O. ?0 G% r" J, |3 H
- dimLine3_startPoint.HelpPoint.Z = 0.0;
1 U, o, D$ b# t - dimLine3_startPoint.View = nullNXObject;
. d( z: b# B: N Z# ?' T - ; ~& {% l: N; t9 r" \+ i
- Sketch::DimensionGeometry dimLine3_endPoint;
. F; c X5 y/ q. y - dimLine3_endPoint.Geometry = line3;
. i, s# |9 \# {: {' w# a - dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
- I8 g/ \+ `2 { - dimLine3_endPoint.AssocValue = 0;
- t, t5 s+ b ^, j- A - dimLine3_endPoint.HelpPoint.X = 0.0;# k; }* g2 h& W5 M# @6 v- R. l0 U2 u; ^
- dimLine3_endPoint.HelpPoint.Y = 0.0;& v* S/ ]0 S/ |+ a" U( J6 q
- dimLine3_endPoint.HelpPoint.Z = 0.0;
$ K p# Z* }+ [. S3 I5 o% _ - dimLine3_endPoint.View = nullNXObject;
/ r" W3 W3 x& I2 A7 a - 4 s& o3 U" w& j1 s
- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);' r& U- E$ Z! q p1 R% Z- G) G
- . A, f8 M7 g' o t. b, [1 [
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);
# [- z5 u/ w$ p: Y$ L! @ - , J/ M) n0 U! Q1 ^
- 5 Y, L% Z8 D7 L. w+ k2 W
/ L0 ?; E% l# g! Q3 p- {- //5 update & deactive
% }( R& Y8 z; N& R5 ]- a - sketch1->Update();' [9 \5 P6 e. S- O) D# l
! a, [( P/ i% V6 h$ C- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);, \ O7 |6 S1 v' x) Q
c, G- U, c% ]- delete expression1;
6 s- }# D1 _( \2 `+ r5 T# O - delete expression2;
; }& r/ Q! X* y6 g. `% W7 r - delete expression3;
/ Y- y1 Z6 n5 [) M - ) v! t9 T* n& d2 f2 d1 y
- }
4 z0 ^: N& c8 ]. Q7 d
复制代码 * K: K/ E- ]" y
( U" `& A" a0 a3 q
: ~% U" Y0 `$ Q9 O1 A: w; ^( s; G4 b) g( B' F
|
|