|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
1 U$ I% e3 _* y+ G内容:7 b7 v/ C, Q4 ]
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。
2 J3 y: e, k$ U3 F/ }% {; O* T+ s4 r目的:4 ~% ^% c2 d# ^; t6 O7 y& k
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
; m% M+ n z5 z& _
NXOpen C++创建草图基本特征
$ d, b( G. L. N6 p8 ~1 b5 R; t. C
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍4 [! `& _9 h* o2 \
31 NXOpen C++创建基本特征-草图的创建(2)UI设计
) L3 i& ~8 P3 {2 r( p7 ? 31 NXOpen C++创建基本特征-草图的创建(3)项目创建
0 G8 h- h, Y$ C+ E4 ~ 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建+ k* A% w0 a/ v o0 W3 x, X
31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图$ T5 Z' J' r5 y* f8 S* f/ v
31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束; q: j: h5 @# z9 r6 \' w) q9 @
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束
! a8 w. [) e$ O8 w! Q. K" _: C' M3 a 31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联
( i9 c% m* X- ]+ T+ }8 ]
3 ^3 k% f& V' K& A* `1 a# I S9 U- E7 x+ v
% l7 `- j: w: H
6 S, D& z2 {" ]$ I! d2 s. w- O, W2 V1 C. p$ p0 c6 F. r
关键代码分享:# M& T5 l2 I, j; P0 Y" o4 x
, s9 W' Q; N. ~+ Z- S0 y
c$ f1 k' l n
# i% X/ j8 l5 G5 O- B- void lesson31_createSkeTCh::createIPSketch()
3 o4 e) s$ N! }. U7 V - {
' a: V1 h5 z, Z5 D/ F$ M - 0 F( S9 Y/ ^! V0 w
- Sketch *nullSketch(NULL);+ c; N( W$ s# ?7 d+ ~: N w
- SketchInPlaceBuilder *sketchInPlaceBuilder1;5 a1 ]' H0 r' i& s
- sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);5 J: W# }) A& l0 n+ o: U7 a" r
- sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);
; ]9 ~/ ^& d) ] A0 w - ( }3 L% |. p, v: f: P' P
- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);/ e3 {9 k* J" Q& H' Y1 x5 ~, }
7 }- \! I0 M1 p' m* d! j4 ^7 g5 I
5 D& \0 T; ^+ t# Y1 Y8 f( E: n- // add preference
& a1 y. K% _' K* j) e7 g3 C - & |8 j2 j0 F% Y' e4 p% e: f
- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);6 ?( q6 H# Q: t! @5 b) c/ ^9 D
- ( V& d! B. K% d9 e. h
- Drafting::PreferencesBuilder *preferencesBuilder1;5 e; M. K, V0 J) l
- preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();
# U1 K$ P6 g* ~ - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);- O. Z4 t) S5 R- i9 m4 W
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
, K3 q. v8 C- H( h - preferencesBuilder1->Commit();' d, o3 C% }4 Y' q8 U) \
7 L1 w% [5 p6 J- preferencesBuilder1->Destroy();
: V; N. N; w' B9 M& T4 L
. n3 W8 z5 w8 Y: B- 3 b: `) ]5 |- t
- NXObject *nXObject1;2 r1 G: L6 z% i0 b; A8 e% P: P
- nXObject1 = sketchInPlaceBuilder1->Commit();8 s% z( B8 X( n6 g5 G* U
- 0 W9 l4 d* G2 L0 V
- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));8 F3 j( q- f, f/ g9 l6 n
- Features::Feature *feature1;
: t9 q4 }; r/ X) r - feature1 = sketch1->Feature();- K6 r( [8 H9 e5 A' N/ b9 d6 u* l
- feature1->SetName("XY-Plane-Sketch");( T2 r% Y3 y+ M' a0 ^2 @9 ~) B
- 3 I s* C1 l }! C, r6 d; s6 f
- sketchInPlaceBuilder1->Destroy();' e* t6 U- z% `/ ~8 @
7 d5 B' r# ], _6 _1 m2 O6 N- //1 active sketch 9 T+ {" {/ C- L& |) ^
- c w, K/ a: j$ r) ]- r
- sketch1->Activate(Sketch::ViewReorientFalse);
+ d4 U4 }& e1 z8 l/ [+ A2 \ -
% }+ c5 `& o# \9 T, B: F - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
, s% E5 y# x4 p7 M9 l8 B5 g - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");3 m% M# c. L$ ^6 l9 ]
- double length = this->expressionLength->GetProperties()->GetDouble("Value");
7 d1 }, J( g( o8 |& G& K - // 2.1 create geometry$ x7 b' c) J8 \: ~! N
3 q, y( s: g: a; { ^7 x- Point3d startPoint1(0.0, 0.0, 0.0);/ c6 X- B/ O2 b8 Y1 A: R
- Point3d endPoint1(0, height1, 0.0);: @- b# M$ J# J5 b" ]
- Point3d endPoint2(length, height1, 0.0);
0 d' n( r5 U4 ]" k- o - Point3d endPoint3(length, height1-height2, 0.0);' _* ]: c9 S- s5 e
- Point3d origin = sketch1->Origin();' O4 i* y. {5 m* y
- Point *originPoint = workPart->Points()->CreatePoint(origin);4 h" }8 o! G+ B& ^: H
- 9 s0 c" V6 w, s' e+ g
- Line *line1,*line2,*line3,*line4;
+ B @! r$ ?# \* g' F" X - line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);
7 E+ Q8 U# C8 y- V* R. h - line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);
7 k: T; c: b/ ` - line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);- l% k! H# K' R6 u8 \! a8 A* k; ^! w
- line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
9 @8 @& |; v9 M. v9 P6 U7 m - // 2.2 add geometry
6 e. d% R6 o. w( T - 8 i9 z" i$ f3 X5 _ Q
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
0 w) x3 m7 k, U( J - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);
/ T9 ?( F* I$ c# p - sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);$ d6 D; i) a+ i/ A9 L- s
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);
+ E' V2 z+ z" `4 w
$ M+ A k, v, Y2 l' B# h8 X- //3 add constraints ( gemo)7 a. ?5 ^2 U' f# I: }+ u0 k
7 @, k, s( T. h9 R: \5 e- Sketch::ConstraintGeometry geomline1;, Q; O8 F+ u: ^7 _
- geomline1.Geometry = line1;
: I; [& {; t+ o - geomline1.PointType = Sketch::ConstraintPointTypeNone;) X9 |9 g+ j2 U# M$ L. s
- geomline1.SplineDefiningPointIndex = 0;
8 D* M, V3 r ^% x$ { - sketch1->CreateVerticalConstraint(geomline1);6 ~! S0 S/ U. n4 k+ ]
- # F! X% V7 A9 e
- Sketch::ConstraintGeometry geomline2;1 Z# u0 m( b, _/ |! @# s
- geomline2.Geometry = line2;
, M2 I& g1 P5 s/ C+ Q% ^& \7 \ - geomline2.PointType = Sketch::ConstraintPointTypeNone;
* S) Y! F6 `4 m& j - geomline2.SplineDefiningPointIndex = 0;; B' S# r6 ]: A w q" H
- sketch1->CreateHorizontalConstraint(geomline2);0 r+ `' E# K) M7 c
; j) h( z# ^0 H8 r- Sketch::ConstraintGeometry geomline3;$ g/ I4 u* R2 f u
- geomline3.Geometry = line3; B) A) Z6 j P+ p
- geomline3.PointType = Sketch::ConstraintPointTypeNone;
4 d4 T1 c. V( ` - geomline3.SplineDefiningPointIndex = 0;
: P) e) X1 c. ~ - sketch1->CreateVerticalConstraint(geomline3);
( L+ p, r+ s; Y5 h
+ B* l# L2 z# W- Sketch::ConstraintGeometry geomline1_startPoint;/ v+ S% j3 ~# M7 M* o; Z: J8 c, O
- geomline1_startPoint.Geometry = line1;3 W8 d4 w6 y- n4 [" L
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;; d8 v) X- d: d9 r# g
- geomline1.SplineDefiningPointIndex = 0;' L, }$ n9 \: p
- Sketch::ConstraintGeometry geomline2_originPoint;9 h7 R- l! c" J
- geomline2_originPoint.Geometry = originPoint;
% ?7 L, e! Q7 v; M7 I - geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
# h3 W& y3 j: a: W0 n9 y' l - geomline2_originPoint.SplineDefiningPointIndex = 0;: `# h% H8 }# [1 j3 G6 e$ m
- sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);
& D) i4 a$ T# r* h- s& I2 c# t - - k: ]0 M/ B! R o# o2 ?& `0 J
- //4 add constraints (dimension)
$ N/ k7 p1 {( U, ^
" [! i$ {* n0 p0 X8 Z9 L2 N- NXObject *nullNXObject(NULL);
: x- }8 d5 _, A+ n/ N" R
1 w4 f$ k% `, Z6 R5 W- 7 |% S- L3 u) A1 j0 J
- stringstream s_height1,s_height2,s_length;
* L2 g1 O+ i+ N& \( L/ d - s_height1 <<height1;) {0 M |+ h& Q9 M$ X3 `1 Y3 ^ a: p
- s_height2 <<height2;
4 X. s7 @/ Z& h! ~0 W% R" ^3 U' G - s_length <<length;" \! m$ n: R% h* q
2 {1 N; K, x1 I7 @! `4 A8 z- Expression *expression1,*expression2,*expression3;
3 x6 }0 X1 {8 D9 J - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
0 o0 U$ y; Y- U; W) B+ G6 ], W - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());, R& x; ?9 {7 N$ g& T- F$ s1 v
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());. `3 w* F6 w: G1 q; [ `, Z7 T" n
- % |; b( u+ S# i) D9 w! m2 _+ f
- // line1 dimension 8 V5 F! U( P% N3 D- N% G7 l
- ! E" o6 {/ G k. d( m% g
- Sketch::DimensionGeometry dimLine1_startPoint;
5 Q) ~$ v( ^7 a& D, a% f0 d' v - dimLine1_startPoint.Geometry = line1;) M, m0 p" W( ~( l
- dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;6 v9 i9 H) F$ J* @) u8 j' i; R
- dimLine1_startPoint.AssocValue = 0;- L! W6 S% x1 d
- dimLine1_startPoint.HelpPoint.X = 0.0;
7 ?$ A; T7 v9 j* D. ?. O" ` - dimLine1_startPoint.HelpPoint.Y = 0.0;& ^ @+ t7 H; h! v
- dimLine1_startPoint.HelpPoint.Z = 0.0;
1 t1 N" B$ h' X - dimLine1_startPoint.View = nullNXObject;8 s; L% T. {0 L* Z) t
% ?2 |5 o8 H3 Z- i. ~/ T- Sketch::DimensionGeometry dimLine1_endPoint;
! o. ^1 E+ O- o! z6 R - dimLine1_endPoint.Geometry = line1;
3 u6 c( K6 }( v' W2 C - dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;
y( Y8 a. G* z3 }- L, h" g - dimLine1_endPoint.AssocValue = 0;
0 p0 @$ ~: Y) h: G4 C - dimLine1_endPoint.HelpPoint.X = 0.0;
2 x& C( B6 N5 q8 f% l5 w0 h - dimLine1_endPoint.HelpPoint.Y = 0.0;. Y% s+ k3 [3 w7 J8 Z* a
- dimLine1_endPoint.HelpPoint.Z = 0.0; k4 J- a# ^; e2 S- F- r" I5 q
- dimLine1_endPoint.View = nullNXObject;) G: N! [/ X) J
. F8 ]' i: i6 T$ }, o. ^- Point3d dimOrigin1(-100, height1/2, 0.0);
% A0 B; t& P8 g8 x0 Z, O - n3 x6 C. M" \$ P- I
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);+ ^+ S3 [( j5 F$ `. C9 o
0 a. ` C5 d3 z) d0 @$ F* p6 Q6 J4 c4 @- ( ?# c9 v! q# z
-
: a+ ^! ] J, F: x0 X/ X - // line2 dimension
+ N2 v" W. F% O. J - ; S0 N! M* \& a& o; O' r( w: n
- Sketch::DimensionGeometry dimLine2_startPoint;
0 d+ d" h5 c/ g, L( F1 x' a - dimLine2_startPoint.Geometry = line2;
: Q5 ~2 A- j. M: S" L - dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;$ a8 f) G( k8 P5 b; r& G
- dimLine2_startPoint.AssocValue = 0;
% e$ K" O( A7 l# L4 T - dimLine2_startPoint.HelpPoint.X = 0.0;1 d6 D8 l4 h+ }2 Q: y: T+ F1 P
- dimLine2_startPoint.HelpPoint.Y = 0.0;- o, |: y. T2 \8 o0 O
- dimLine2_startPoint.HelpPoint.Z = 0.0;
% g1 j; Z F6 l1 | - dimLine2_startPoint.View = nullNXObject;
7 \5 F' G) h# H' `+ S! l
6 Z- a, S) b9 ?, Z9 D3 F- Sketch::DimensionGeometry dimLine2_endPoint;
1 M) Q& Q% i( N9 V9 S$ \ - dimLine2_endPoint.Geometry = line2;7 l1 y! [- v6 H: x
- dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;
" d, R5 g) |- x7 e - dimLine2_endPoint.AssocValue = 0;
* f* ^( }0 |6 U - dimLine2_endPoint.HelpPoint.X = 0.0;; L. r4 S$ {" ~5 d* @
- dimLine2_endPoint.HelpPoint.Y = 0.0;
9 Y% @: c( A! }) ^1 s - dimLine2_endPoint.HelpPoint.Z = 0.0;
5 V* g# P" R x4 Y - dimLine2_endPoint.View = nullNXObject;7 W1 w0 \+ T% U; z1 W# N8 B/ [
( @, ?2 q4 I8 P% N* u- Point3d dimOrigin2(length /2,100 + height1, 0.0);
; Y. @8 P" Y+ o
3 _8 ]% Y( Y, x- y8 Q2 G- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);, t1 X* m0 R! t/ c
; Y/ p, c4 \( j0 c
8 K: @: g: k& g( ~-
: c( _1 C! s' i/ M; U. N5 X- F - // line3 dimension
, ^* U5 w' i+ p9 f - 3 ^! Z; n, u0 Y' p# T5 V. E+ Q
- Sketch::DimensionGeometry dimLine3_startPoint;
. |' R+ Q# j0 L5 t. c c u3 r - dimLine3_startPoint.Geometry = line3;3 `! Y q" s% b" q5 p) M( I
- dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;) M- O, }! f9 j4 z) N) d7 z
- dimLine3_startPoint.AssocValue = 0;
{8 e, y; a+ U$ P' @; T% K - dimLine3_startPoint.HelpPoint.X = 0.0;
: E7 U+ q8 r' } - dimLine3_startPoint.HelpPoint.Y = 0.0;9 Q, ]/ x9 |' I; {
- dimLine3_startPoint.HelpPoint.Z = 0.0;1 Q2 T5 W# S( E9 |% a
- dimLine3_startPoint.View = nullNXObject;
% S6 U5 P1 _9 R& e `) I) I" K# m$ E r
! x6 k0 Y- y# l% b7 B) p- N- Sketch::DimensionGeometry dimLine3_endPoint;
* W( u1 s( N9 F - dimLine3_endPoint.Geometry = line3;9 @% _4 Z4 ]# Q' ` N4 s/ ^
- dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
- U6 ~, G2 r6 w' j0 v) d C - dimLine3_endPoint.AssocValue = 0;$ ?$ g( m* E4 m4 c5 ]6 U
- dimLine3_endPoint.HelpPoint.X = 0.0;
8 D1 o: O% M# Z - dimLine3_endPoint.HelpPoint.Y = 0.0;
' O/ u" Q$ l) P/ @3 N - dimLine3_endPoint.HelpPoint.Z = 0.0;
Y8 k8 g, j3 y/ M% o, ]+ D - dimLine3_endPoint.View = nullNXObject;
2 e9 f" r$ D+ |( l! X - # q, k2 C9 M3 T6 H8 h" r9 v
- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);/ u7 ^8 S. B8 W) P
-
- H/ y$ E" z' n8 h/ n n - sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);( q$ C8 z/ z- y8 q/ H
# @8 ^0 l7 d1 R
) F* g2 B/ g5 \6 `" i* I/ O- : T7 C" s S4 r
- //5 update & deactive
9 X8 t# e) Y2 E7 \" y1 F8 b - sketch1->Update();
. N# j( W: G* p/ I- f! W
" Y9 o/ |* @+ P- e4 n4 i- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);) ^8 H. r. H2 O O4 z+ o0 m% g
- 1 r9 s* l/ w3 o( }
- delete expression1;
* S/ |, D: I' ]5 N7 k% c1 x) l - delete expression2;
( R; `, j' V- t: Y; t& b& O - delete expression3;
" ~# e6 C, C$ X, @) A -
" e( r# ?! q' B' K& V - }
! P- y7 L( C" w6 ]* _
复制代码
9 \: w3 u6 k& y
: Y: s: D) |. U# ]: F6 ~9 G6 C! ^ g# K6 S( s6 q! |
/ w1 L) a+ a- {3 e$ K |
|