|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建2 n l) m2 b0 T2 s' R6 q, b) [5 C5 @
内容:2 Z& j8 U8 Q* y/ n
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。
0 E p/ O7 w1 ?6 e2 T5 D* ^; V目的:) q* z# L& V! y" m& x
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
$ r# X% H7 M& G( K, [- G
NXOpen C++创建草图基本特征
2 Z! k& w* z; { 31 NXOpen C++创建基本特征-草图的创建(1)项目介绍
4 Z0 d& d C5 p* k 31 NXOpen C++创建基本特征-草图的创建(2)UI设计
: Q* D, ~ n: ~5 r 31 NXOpen C++创建基本特征-草图的创建(3)项目创建
& l g) w& w! }7 @: r' y( E: P 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建7 a1 F8 M) x0 T' H0 } e" z
31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图
2 Q' W& n% X/ B, t- F( h3 t+ j 31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束 D( Q: V0 d6 Q# V( P
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束: O2 c2 k4 @6 N8 i$ ]/ w" `. r
31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联' m4 i4 t' j: Y; ]
% Z- }$ r+ `) F q
/ T+ P* ]5 L U2 p# [
( H* e5 H- R2 [5 n% y ^1 _ H6 p, `( G2 _8 G4 c0 g
: ]/ G7 @+ G/ G) o# N `关键代码分享:
# D U+ f, K6 N& B3 U
2 i ^! d4 u$ a- h% d( z! J( J8 e# v4 ~, W
- 0 _# y4 R9 A8 r6 O0 X, e' P
- void lesson31_createSkeTCh::createIPSketch()9 |5 W% g! j/ H% P
- {
# l$ A2 H3 l: E: ] S6 U8 [
- f: B6 b) S0 u- Sketch *nullSketch(NULL);9 l1 |. c/ Z F0 F+ l {- X) ~
- SketchInPlaceBuilder *sketchInPlaceBuilder1;: S2 M7 {' Y9 [& W6 b0 S6 o
- sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);3 v. v' U9 r- z+ o' i5 B% x
- sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);' G' M) A7 }; R1 s( d( D
4 d" ]8 L N h- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);
* `" ^/ a, ?: g+ i9 A - / ]0 ]' r( |# u* x2 ?5 z
- 2 S6 {0 h0 @5 q; M2 L/ L; a5 X4 X3 s
- // add preference) }6 o- F; y0 p% O/ X
( z0 A( L1 H$ H4 y- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);
, Y4 r! s# j. g$ X# n8 j4 R1 C - - O0 O, m1 @5 d3 D6 q
- Drafting::PreferencesBuilder *preferencesBuilder1;& H- k( m0 w; K9 D( m; A
- preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();- q) v# C( `& {( X$ I0 E
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);2 R9 T9 s% T5 g" f
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
& E' q0 p o. o" O2 g- j - preferencesBuilder1->Commit();: K$ r8 o& R k0 W! d/ f# P; i+ A
0 h# M, _4 _! m1 B2 A- preferencesBuilder1->Destroy();
# l0 P7 M; s- m - ) d% P1 U( R! c
+ \ l: _- J+ \( s- NXObject *nXObject1;
# x7 t0 V' t$ n - nXObject1 = sketchInPlaceBuilder1->Commit();
, H/ n9 Y, v; { g2 m, t
% C8 S0 y4 |' I9 |6 D3 ^/ f- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));9 o8 O2 L- _7 Q, Q' P0 ^ g3 ^7 x
- Features::Feature *feature1;
: r1 W! }! r, X - feature1 = sketch1->Feature();
" ]8 Q2 ~ `* p+ b# s! m# Z - feature1->SetName("XY-Plane-Sketch");; _/ \* U4 Z6 V1 G! N
-
5 _' I5 k+ J& ^* l! _ - sketchInPlaceBuilder1->Destroy();# l6 a# g' Z/ \; H0 J; S e
+ R, o, t. U9 r# I$ Q- //1 active sketch
# j7 q6 u$ A7 a4 K- F9 x, z2 b
" C1 c( L; B: v5 } F. H; ^6 p- sketch1->Activate(Sketch::ViewReorientFalse);8 A* Y6 n; f5 K9 O! A' `
- 1 C3 }) L, R; y' k' b* @
- double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");1 `2 Y4 h: F* A& ^) X
- double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");! S6 \5 s: d' V0 y4 K! z# a
- double length = this->expressionLength->GetProperties()->GetDouble("Value");
: X: Y a* l7 k4 S2 z# u' Q& z2 ? - // 2.1 create geometry3 C/ a ~, q( k% r) `
7 N( Z. V9 z* k2 w3 K- Point3d startPoint1(0.0, 0.0, 0.0);
1 H) d- A* M, p' Q& J7 X$ h - Point3d endPoint1(0, height1, 0.0);
2 H1 X' A ?. H! s5 U0 c - Point3d endPoint2(length, height1, 0.0);
8 t; |$ q- F* i# Z! C4 B - Point3d endPoint3(length, height1-height2, 0.0);$ o. K( P1 g6 r0 W" H
- Point3d origin = sketch1->Origin();
+ }5 z# u1 u' a( d, v1 M - Point *originPoint = workPart->Points()->CreatePoint(origin);9 ?6 L% P7 [, q& _0 ?1 N8 L
- - t/ g0 ]( U# P/ c* [
- Line *line1,*line2,*line3,*line4;5 \0 J) D* n C1 T3 [; P( U
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);: g9 r2 b/ h* N3 i# z
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);# E1 @7 T7 n! G9 Q, _
- line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);
' T8 L$ b# i0 h$ G+ b- P$ D. V, L - line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
& @0 G0 F6 Q+ z8 b2 c - // 2.2 add geometry
7 Z4 ]% u2 g B' V( i: q: |% V - 7 @& L$ i" H) E: y
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
' k, Q7 w2 u2 Q6 s - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);9 h' E L2 H$ N! d" @2 m
- sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);9 A4 F* x2 y" X! v5 z9 f# j: k
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);( Q, X$ w( i# ` k
- , W' ]9 h# K% R% m
- //3 add constraints ( gemo): {& l9 W2 q+ f% X1 s& z- Q: V W
- # v J, v; H1 |# x# _3 U
- Sketch::ConstraintGeometry geomline1;& c9 s) `' \! M7 \
- geomline1.Geometry = line1;
0 {, O$ q C$ R9 i: ^ - geomline1.PointType = Sketch::ConstraintPointTypeNone;
, j7 w' Z3 L" q - geomline1.SplineDefiningPointIndex = 0;+ @: p' X, v/ D4 u* k
- sketch1->CreateVerticalConstraint(geomline1);, k6 _% ^! g2 c4 T( O
* [( @% k0 o. G, `& E' _' n- Sketch::ConstraintGeometry geomline2;
$ q9 C G. ?2 P: E; X; g% L! | - geomline2.Geometry = line2;* |: P2 R% j# J: S
- geomline2.PointType = Sketch::ConstraintPointTypeNone;8 r% a: V9 {( W" W
- geomline2.SplineDefiningPointIndex = 0;
. l! y, n6 {; d/ I- P. k - sketch1->CreateHorizontalConstraint(geomline2);7 U# s3 p! n2 ?, D
- 6 W, w9 X; W2 X, J
- Sketch::ConstraintGeometry geomline3;% k: V# M% V& o1 Z+ n* l2 {
- geomline3.Geometry = line3;
2 S5 @$ L: V, h" W - geomline3.PointType = Sketch::ConstraintPointTypeNone;1 _6 F) p# T2 o- J* z
- geomline3.SplineDefiningPointIndex = 0;
9 X+ J T( x* m* A - sketch1->CreateVerticalConstraint(geomline3);
, X$ ]6 ~& B; R: V - 9 Z+ P3 ]4 E$ e; t+ [
- Sketch::ConstraintGeometry geomline1_startPoint;8 D# P6 b w# A% U% ]1 S# x d3 E
- geomline1_startPoint.Geometry = line1;+ } q/ R. Z; U H; R6 i; d) I9 @
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;. c0 N! C, S2 L+ r" W9 J( l
- geomline1.SplineDefiningPointIndex = 0;/ |& d4 u6 ~" W
- Sketch::ConstraintGeometry geomline2_originPoint;/ A9 b- m5 o! d3 H3 T1 B: o7 u
- geomline2_originPoint.Geometry = originPoint;
8 M$ n! r+ r. b1 p, s - geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
. c3 D/ O- X- Q% T! E - geomline2_originPoint.SplineDefiningPointIndex = 0;
8 G' [. }' d/ T* r! ]5 [ - sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);% Z( T. q: Y2 `2 g$ p
- , b. Q0 l# p. q4 P& _2 ]
- //4 add constraints (dimension)
- I9 O: o8 K, A C9 H
1 R+ ~( B% z% b! F" T0 l" N- NXObject *nullNXObject(NULL);
0 b- T) g2 U0 r! A
& M. x0 F2 q$ q, e4 G# ^
! i% F0 Z8 b1 Y2 g* t- A6 x% b/ J- stringstream s_height1,s_height2,s_length;
" M7 u3 Z1 g$ w6 Y1 C - s_height1 <<height1;3 H0 l2 }1 b3 r) L
- s_height2 <<height2;3 Y( Q6 d; Z3 y
- s_length <<length;/ M; @9 S) d! ?# n( ]7 e7 a
- / q" ~# ^# I% I
- Expression *expression1,*expression2,*expression3;
2 F3 D" z0 B4 t; | - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
% {# N* b, f# a Z9 }" F& S# _ - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());/ B" ] n! x. O! |$ w
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());
" r" _5 F& Y) b) {6 B3 g1 T8 M
( I+ ]7 r q5 i+ ]6 F- // line1 dimension t F0 p" F1 W1 a
- 7 v/ G g I" g2 K" W- N/ a4 R
- Sketch::DimensionGeometry dimLine1_startPoint;
$ L f! k& S" \1 W3 ^) A4 j - dimLine1_startPoint.Geometry = line1;/ o3 I, F" C- ^$ g
- dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;( V: ~* d' {" n4 N8 }7 t% ?7 V
- dimLine1_startPoint.AssocValue = 0;
) v) k0 h4 o3 J# X9 [$ n) L - dimLine1_startPoint.HelpPoint.X = 0.0;
0 W5 P) @5 A9 V - dimLine1_startPoint.HelpPoint.Y = 0.0;
: b8 q( \4 D# R) f0 O - dimLine1_startPoint.HelpPoint.Z = 0.0;9 [1 W# N& D) Y' i. V6 m, z% P
- dimLine1_startPoint.View = nullNXObject;
' q( C3 ^, ~0 o$ ` - |; x% Z- ?9 N$ C2 s- j2 j* g# b4 m
- Sketch::DimensionGeometry dimLine1_endPoint;' t. M/ ]& V7 C& V c1 f- n
- dimLine1_endPoint.Geometry = line1;5 J5 G- K3 G# W+ ~; _$ R J
- dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;
V$ N! K% ?+ y- r/ @. k - dimLine1_endPoint.AssocValue = 0;
2 c+ p: U! K+ H2 n8 a! P' U - dimLine1_endPoint.HelpPoint.X = 0.0;
/ r; }" |8 \. \4 v7 H; H - dimLine1_endPoint.HelpPoint.Y = 0.0;
9 J) e# i9 I; }* R4 o# i' u& _' M8 o - dimLine1_endPoint.HelpPoint.Z = 0.0;
$ F0 b9 v/ \2 T& T$ S; {& c0 z - dimLine1_endPoint.View = nullNXObject;
: M( b4 W4 p- D ^3 ~
1 E* j- ^: P; K/ Q4 \- Point3d dimOrigin1(-100, height1/2, 0.0); b3 S# M, Z- k5 r0 l
" Z' D) i g' R9 P; E- e- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
+ ~' i+ h' P3 J. ~$ q, [7 j - 1 h R' E+ Z0 w/ r3 Y: {5 O/ u
7 V y# w' v# Y- e% [- 8 I Y+ }0 s, g( e, } _" H7 ^
- // line2 dimension " e, B! K5 S3 b$ R
- , H! P- _1 o* }8 ^
- Sketch::DimensionGeometry dimLine2_startPoint;* W Q; K2 [6 s! V7 M
- dimLine2_startPoint.Geometry = line2;
: h) s* t. G) M - dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;
7 f. D' c: N7 e( Q+ L$ I+ X - dimLine2_startPoint.AssocValue = 0;& }/ F/ p& o' B) Z! S4 |3 o+ B
- dimLine2_startPoint.HelpPoint.X = 0.0;# E1 |- t" r- t; c/ x s& l
- dimLine2_startPoint.HelpPoint.Y = 0.0;
; l- U+ l1 |% M' K/ h - dimLine2_startPoint.HelpPoint.Z = 0.0;
$ k& D) l# j+ P" s - dimLine2_startPoint.View = nullNXObject;
( f: C: b3 ^: H/ s: } - 3 L6 Q3 d3 v) T2 x8 E. a/ G
- Sketch::DimensionGeometry dimLine2_endPoint;0 y2 G9 j: G+ u9 a( G% |2 C$ C
- dimLine2_endPoint.Geometry = line2;6 u `! u5 `6 }2 d+ x; I
- dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;
- r9 O3 m; a: N( s5 N' M# W - dimLine2_endPoint.AssocValue = 0;, P. b/ o8 n* ?1 T) ]- J
- dimLine2_endPoint.HelpPoint.X = 0.0;- |3 f5 Q, r% c8 ], X
- dimLine2_endPoint.HelpPoint.Y = 0.0;" G5 P* J. p' a' t$ v
- dimLine2_endPoint.HelpPoint.Z = 0.0;
% H3 x0 w3 q2 }: n0 d( r* Y ?: V; } - dimLine2_endPoint.View = nullNXObject;
3 y2 Q4 w- j: l - 5 g. K8 g, |1 d0 A8 ]" g6 ^, J/ m
- Point3d dimOrigin2(length /2,100 + height1, 0.0);, j0 o B* Y: Q# v6 W
- + t/ e% X/ ^$ V9 a+ G. F+ Q: B
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);1 Q# O# [) G2 |! K
5 ^$ Y2 n1 M e; k+ T# J
! {2 e7 O* @' p. J. X7 A-
' m4 a$ u' g9 H3 l; x - // line3 dimension
& i) f: \8 E! `3 Y# _$ s - ( u: u5 k4 o7 _) D& |6 ~/ x( u
- Sketch::DimensionGeometry dimLine3_startPoint;& W, x( ]. j6 P% L. h
- dimLine3_startPoint.Geometry = line3;
" B/ _: [- e# h. o7 m - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;
, Q9 ]- F% J: u& @ - dimLine3_startPoint.AssocValue = 0;; `: o, f/ i& j, Z R+ r
- dimLine3_startPoint.HelpPoint.X = 0.0;
1 P# U6 t& `1 s% `# A7 ^# [+ a - dimLine3_startPoint.HelpPoint.Y = 0.0;
+ k3 F, m9 `; u# L% u. l5 Q% I - dimLine3_startPoint.HelpPoint.Z = 0.0;; k4 c: F1 B5 t5 e
- dimLine3_startPoint.View = nullNXObject;( O. b; I7 u& [6 [* c* {
; \& _4 v3 M, y2 ~" t# `) y* Y- Sketch::DimensionGeometry dimLine3_endPoint;
& ~* x( |7 [- r% a - dimLine3_endPoint.Geometry = line3;
/ E! S# o. a7 I$ J7 H# G* z( E - dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
1 y% y& |7 b+ r; n - dimLine3_endPoint.AssocValue = 0;: b6 N0 ~* z2 ?6 |" r
- dimLine3_endPoint.HelpPoint.X = 0.0;
: m; w6 D& A! f3 R+ p! b- h - dimLine3_endPoint.HelpPoint.Y = 0.0;
9 p2 E4 ~) y, z0 J* X - dimLine3_endPoint.HelpPoint.Z = 0.0;
* O8 t5 k% f. i, w0 b - dimLine3_endPoint.View = nullNXObject;/ c- Z v( @& @$ i
- ! ], m* l! {+ p" C
- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);: y) }( Y B9 {1 `
- ( K, \5 N4 s7 `- c. u2 l
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);
. b: c1 ^9 O( B+ B& W3 v* n
' ~" n0 `6 p/ w u
* G6 x) ~* O o7 K, t5 p
$ {& T% m, F4 U9 N9 f, Q1 I) Q0 q- //5 update & deactive
7 R- p& @# D; c; ~7 d5 z2 G - sketch1->Update();
; M% p$ J8 \/ P# [ - 6 s2 m* P) {% `6 R
- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);4 R/ J# i6 d9 z! T
: U, H. a5 b2 j6 k* _& m- delete expression1;9 }# \$ m4 B) \' C1 Y+ G3 H3 P$ W
- delete expression2;
$ ]* X8 \3 B$ p% \, @/ A - delete expression3;0 F ?2 c( M& m. T7 a
-
* M4 s- y0 P- J& l - }% I5 W) f) m2 ?, G* X" l6 K
复制代码 L# `/ ~& Q* }# H) `2 C% l
6 _) j/ x. K4 `1 `; y9 ]
* c' s, \3 D2 D" K. O, P# Q4 T9 Z! f$ p( O4 J* k, P
|
|