|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
( b. O" P5 }& M7 `6 j# k$ |! `内容:
# g* R; R: U9 o- k5 j草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。
: W3 r& p" f$ E2 x目的:! w; _- _+ Z* C7 R. f3 J5 y5 k
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。- u" _3 X) i, F: |, k( e5 Z! q
NXOpen C++创建草图基本特征
% B; S) m: E& _# M8 R w
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍
8 G) O; Q' S6 H" c! t 31 NXOpen C++创建基本特征-草图的创建(2)UI设计
4 W8 I& E' [* m+ q% ? 31 NXOpen C++创建基本特征-草图的创建(3)项目创建
3 `! u* z" B- a2 k) b! K 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建0 ^+ x+ ^9 D& M3 g2 V6 {
31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图
2 Q5 a$ S# P) Q: i7 L 31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束2 X' E; i, V C6 C4 `
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束
, { V9 W$ ^$ [8 N w" x 31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联! O% I7 w$ F) r6 e* l* r. Y: H
9 v3 S0 X. z: H
8 t6 \8 i# I( B2 Z8 @! {+ e8 Q; v
/ U* _2 B4 e, m6 ]# z- u6 _9 ~4 n) Z" x: a
关键代码分享:
3 R2 a2 K7 T4 P& g: w# ?% ]6 b8 L! G
& L' O E0 J5 a+ ]# h8 C7 N. \
1 d3 F: S& A: p b; K- void lesson31_createSkeTCh::createIPSketch()
2 Q$ m' r) B d - {
1 c" l+ p8 `6 P ]
/ w( o+ R5 x( X. r4 m0 q- Sketch *nullSketch(NULL);
& S. o- i& |, A8 L3 V+ W - SketchInPlaceBuilder *sketchInPlaceBuilder1;
' t) a- Y6 y% t) |- f4 | - sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);
- `4 p6 o" t i% a9 ~2 R5 u9 O$ s8 n - sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);
: G9 p; P4 x: O: X4 n
3 |( K+ P$ U* i4 T& Z% A# R( |- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);+ n" i( x$ D v Q# F5 K
' B( o0 b+ d6 s- 9 Z7 v+ ]3 a4 M; c. F& e5 R# D
- // add preference
6 {" A# T3 Y! L: F! I }
# f9 t F* ^; O2 w4 t5 P- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);. U' Z9 v" {7 ?* z z
: \0 v. c# V) A: R; U- Drafting::PreferencesBuilder *preferencesBuilder1;4 P0 ^1 ?4 Y8 c# B$ j; k3 X6 k2 l
- preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();5 N# e- w6 [" H: v7 F5 S: Z
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);
6 ?; [$ T l+ W - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
' f( Y5 R: D' ]) Q! b1 F t - preferencesBuilder1->Commit();
+ j% u3 Y T; ^' |% U2 g
' J7 l- Y! L ^% y( S& C- preferencesBuilder1->Destroy();& }5 i' e$ [) k" t- n' c
& V6 q' e1 R6 l6 T/ t6 S8 g& p7 ^
5 }0 j7 G6 l$ t! D- NXObject *nXObject1;
8 g% e2 l/ ]' W - nXObject1 = sketchInPlaceBuilder1->Commit();4 a% `. n; n. R' T) Z& E
' ~. ?# a y' `- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));3 e3 p( W d% u- d# V
- Features::Feature *feature1;+ x+ Q8 H# x7 \6 C
- feature1 = sketch1->Feature();* b6 i0 O# |' E6 \5 L1 ^9 c
- feature1->SetName("XY-Plane-Sketch");
' F3 H# ]( D$ Y* C' I - . B) W& c7 C& _. a- |
- sketchInPlaceBuilder1->Destroy();
4 u$ O! q! o1 l( o4 L7 p% l# ~5 Z8 k
: f( L" y$ N7 z0 q$ U2 I- //1 active sketch # Q E7 o" a- N. e9 i5 Z. f
- " Z! n$ p ?, X. {6 s( ]3 _
- sketch1->Activate(Sketch::ViewReorientFalse);
9 D$ R6 ~2 C- M* ?- K4 A - / P! F. _4 A+ P E9 o3 D
- double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
) ?' m/ w6 Q) q A/ K - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");
2 K4 Q4 g2 X9 n/ y; \$ I/ j$ }% { - double length = this->expressionLength->GetProperties()->GetDouble("Value");% }- }, E0 g* r; j
- // 2.1 create geometry
- g- v' N4 \% z- k7 U
% B, e8 X/ U+ O8 h4 i6 k# f- Point3d startPoint1(0.0, 0.0, 0.0);' r- t# e1 i }# Y- ~2 K; i
- Point3d endPoint1(0, height1, 0.0);% x" L M1 a# Y* H" k6 U; Y) t/ {
- Point3d endPoint2(length, height1, 0.0);
( U# @$ T/ Z* g2 ~+ l' Q# z y - Point3d endPoint3(length, height1-height2, 0.0);7 A- L6 w% }- n( u
- Point3d origin = sketch1->Origin();2 @+ L# S* e$ O6 O& W) w9 c3 [
- Point *originPoint = workPart->Points()->CreatePoint(origin);
. p6 E# @: I' j2 k( R1 L - ; o! p( Q( n3 \1 Y2 e) ~5 l
- Line *line1,*line2,*line3,*line4;
( p* C) g3 h' \( x - line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);
4 ^, }( N2 F1 a - line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);$ D, E- a# C k; g; u
- line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);
1 z- S3 h f8 n6 } - line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
+ ?* n- A" E" P9 C; ]; i - // 2.2 add geometry& H* F6 L' S: _" I/ c2 f- ]
4 _+ m2 T0 K) ^; v7 z5 D- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);+ m" V% l& p! B- J$ [2 C
- sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);
. w5 j/ |+ I! T, M - sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);7 U' W0 f, M: Q$ F6 M
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);, k5 f6 f$ l& a* H" V3 J8 N
- 8 r2 D! @" g9 P5 \% v% s
- //3 add constraints ( gemo). s5 R9 r% p/ t, ^, g( {
% v8 T+ t4 T- z# t! n( P+ C& i$ v+ ^- Sketch::ConstraintGeometry geomline1;
# d! ~7 y) l. N9 Q - geomline1.Geometry = line1;
$ r& W: i$ d. A# G+ W - geomline1.PointType = Sketch::ConstraintPointTypeNone;6 b" m4 f2 f: D
- geomline1.SplineDefiningPointIndex = 0;
6 u7 \, M6 t" {. e3 b4 _ - sketch1->CreateVerticalConstraint(geomline1);
6 u1 u. e% u2 z) q - ; e! N) ~0 h5 V7 ^( E
- Sketch::ConstraintGeometry geomline2;3 @% l' o- L5 h6 G
- geomline2.Geometry = line2;
4 r" n. t* I& O& t - geomline2.PointType = Sketch::ConstraintPointTypeNone;
) _5 l+ }8 u; I2 b' Q; | - geomline2.SplineDefiningPointIndex = 0;
: y& h! k9 D7 E! W- _ - sketch1->CreateHorizontalConstraint(geomline2);, [1 r0 {/ [. d9 Q! z. v+ B6 w
- 6 U, q/ g, i" _- }- ?9 n7 s
- Sketch::ConstraintGeometry geomline3;+ s* k7 l/ I# ?7 P3 g1 I$ X
- geomline3.Geometry = line3;- y. s2 h9 L$ Y
- geomline3.PointType = Sketch::ConstraintPointTypeNone;+ C" Y) l6 S7 M8 L8 k5 q
- geomline3.SplineDefiningPointIndex = 0;* a/ y% O6 g8 [ ~& b$ Q
- sketch1->CreateVerticalConstraint(geomline3);# t0 O k( _+ x2 U( e, N+ ?8 f
- ) i0 Z6 g2 N5 S& G" n, }) y) z0 q6 v3 n
- Sketch::ConstraintGeometry geomline1_startPoint;
, W( q/ O! Z: H# G6 F* S - geomline1_startPoint.Geometry = line1;; y Z1 Z+ I1 a# B2 x
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;
, U. h% D" U# Z9 E$ k- E$ P - geomline1.SplineDefiningPointIndex = 0;0 z6 f6 j" _& ?- [* S8 k8 U, S
- Sketch::ConstraintGeometry geomline2_originPoint; U- g# \% V2 ?0 N3 k! v
- geomline2_originPoint.Geometry = originPoint; a9 `1 n; D, x$ S- X- m7 h! z
- geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
s z/ C5 m5 \% ~8 A$ A - geomline2_originPoint.SplineDefiningPointIndex = 0;
, z, ?+ u) q6 @' J' ^ - sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);
4 Q! T# b* A% A. ? \
0 Y! Y' W/ o# N$ u( d/ G- //4 add constraints (dimension)+ k: \8 C" K8 z
+ i; M+ h6 {+ N" }& _- NXObject *nullNXObject(NULL);
9 {5 T; {/ d3 D2 }" `3 O, p# i - " r: Z& V, F9 R, C* o
- 0 T9 T! h/ }8 U3 M; ^
- stringstream s_height1,s_height2,s_length;
( N6 r, I5 d* C$ b: n( b, {& L% g - s_height1 <<height1;
q' r' y4 s8 j4 P - s_height2 <<height2;1 b: H4 m" F" ^* N3 L+ \2 \' \; ^7 n
- s_length <<length;
2 r1 I, A" N; K9 J$ c) G" p6 T - / K. _. T# c3 h
- Expression *expression1,*expression2,*expression3;
: J/ ~4 q% h& U+ J( } - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
. l1 R" a" \! T! s3 j6 e8 e, G; Q - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());( J9 c6 P8 N+ X0 T+ B
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());
6 _5 T9 D4 K! F4 g
. s- w/ S" r/ ?* m" b- // line1 dimension . Q) F" r3 F& E; y: m- M, }
3 u7 ?6 }5 ?5 r7 d5 F- Sketch::DimensionGeometry dimLine1_startPoint;- s! ^7 M. @7 i2 {8 G c1 T) H, o
- dimLine1_startPoint.Geometry = line1;
( a( A d" b$ J8 Z: k7 f - dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;7 P4 R# U0 D( e" g
- dimLine1_startPoint.AssocValue = 0;' L$ R" ]) Z9 T9 W l) y
- dimLine1_startPoint.HelpPoint.X = 0.0;
4 b' | m, d+ J( w/ s9 w3 ?7 ` - dimLine1_startPoint.HelpPoint.Y = 0.0;+ f9 Z5 t: o4 Q8 a
- dimLine1_startPoint.HelpPoint.Z = 0.0;
4 d/ t( J+ T+ g9 P" ]. C, \ - dimLine1_startPoint.View = nullNXObject;! \: e2 X7 Y9 Z3 y; R; c
, O: V5 y; R: @) X" p0 K- Sketch::DimensionGeometry dimLine1_endPoint;3 U. a' M/ N7 l( Q- r* u
- dimLine1_endPoint.Geometry = line1;" u" K- d, ?$ r7 e" }) \" ]
- dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;
2 X! f8 o3 Q) ]0 U- x8 b - dimLine1_endPoint.AssocValue = 0;: g5 p- A# I, T6 `8 O" ~
- dimLine1_endPoint.HelpPoint.X = 0.0;+ [5 r" @6 D t& O, i' J
- dimLine1_endPoint.HelpPoint.Y = 0.0;) ~6 W. |3 ~0 w" Y/ A2 h7 D/ m
- dimLine1_endPoint.HelpPoint.Z = 0.0;* X9 x+ f, }7 T# q$ a1 E0 o
- dimLine1_endPoint.View = nullNXObject;& ~+ P+ w# h' `2 C% c
! _; Y3 {! l1 y/ p7 Z0 E9 O- Point3d dimOrigin1(-100, height1/2, 0.0);; S$ [. O; X0 q8 O6 |& }: U
- ) t8 Z8 Y& H+ E
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving); K: D0 Y0 u u6 u. u
4 t3 w- [3 F6 B6 p- $ r0 U. b, }$ s8 z }8 n _
- $ @* Q3 x' q% A' t3 N$ l5 ^; |
- // line2 dimension " Y# J/ H) \# p, [( f
* e2 I/ c) e% M, H# ]- Sketch::DimensionGeometry dimLine2_startPoint;
" R# S! j; `) }( ^+ A& D - dimLine2_startPoint.Geometry = line2;
0 d' V2 m. l" n; c" I7 ] - dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;1 ~' ?* U4 \5 i: C4 S# S2 H
- dimLine2_startPoint.AssocValue = 0;4 b( q0 v. O9 |4 z2 E
- dimLine2_startPoint.HelpPoint.X = 0.0;
2 |' P9 H8 _& U1 ~. i* y - dimLine2_startPoint.HelpPoint.Y = 0.0;4 f6 l# e7 N, |0 O) a7 k4 t# N
- dimLine2_startPoint.HelpPoint.Z = 0.0;
5 C5 e g. `- E - dimLine2_startPoint.View = nullNXObject;
2 }( a! [& _1 a l - : N/ J/ }( D8 G" U0 a! Z) t
- Sketch::DimensionGeometry dimLine2_endPoint;! Q# ~% b9 }4 N9 {% o a
- dimLine2_endPoint.Geometry = line2;. u0 v" n0 A+ N$ w# T' z- P4 r
- dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;3 v% J6 d9 j! s9 Y
- dimLine2_endPoint.AssocValue = 0;
+ o7 `# |6 y" B4 i! l - dimLine2_endPoint.HelpPoint.X = 0.0;9 l+ x% s. n5 r7 |
- dimLine2_endPoint.HelpPoint.Y = 0.0;
% K3 T& j( ^8 P. y4 e8 l3 N - dimLine2_endPoint.HelpPoint.Z = 0.0;
( Y+ J4 \5 c7 k* `7 ~ e2 ~% g$ }' r - dimLine2_endPoint.View = nullNXObject;
# o5 z( F0 z1 J- S w4 ^2 g4 j - 2 m! T8 ]. {5 B# T1 z! z
- Point3d dimOrigin2(length /2,100 + height1, 0.0);
, s; a e( H+ ~6 y5 |
: O6 f6 K3 o: P# `3 r5 d) h- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);# a9 l. b2 d0 g5 P
- + G# P/ ^! ]( |/ a/ T* i# y5 n$ h
; D( {! i! _0 d Y* b/ ^( P3 c-
! X6 [# ^6 g. n( u6 l8 t - // line3 dimension
" ?& [6 M x( i2 k1 [9 N
) H6 Q7 R8 L R6 ?- Sketch::DimensionGeometry dimLine3_startPoint;( _4 n5 b2 x7 _+ a
- dimLine3_startPoint.Geometry = line3;
! }# y! u. ]" h; D2 `; f - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;0 [6 i: X/ Y1 l6 a! R/ C
- dimLine3_startPoint.AssocValue = 0;
3 n6 S6 A9 |( }: L v# l9 q3 R - dimLine3_startPoint.HelpPoint.X = 0.0;$ }& H0 z6 E$ {- B7 p1 y" }
- dimLine3_startPoint.HelpPoint.Y = 0.0;8 _. Q) J0 I& H8 P9 a( t. d
- dimLine3_startPoint.HelpPoint.Z = 0.0;( ^7 x4 B, j' c: `8 W Z7 k
- dimLine3_startPoint.View = nullNXObject;
) Q* B7 b9 L1 z7 s$ t - $ a4 L# b) e# @
- Sketch::DimensionGeometry dimLine3_endPoint;
' ?1 `7 b! U, r% E - dimLine3_endPoint.Geometry = line3;. Y) k. v- @' t" L
- dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;8 v5 A a% H& B: {- I
- dimLine3_endPoint.AssocValue = 0;
6 N2 [" N6 h9 P @3 c+ k - dimLine3_endPoint.HelpPoint.X = 0.0;
0 V! m- ?. I3 z2 X - dimLine3_endPoint.HelpPoint.Y = 0.0;
9 l) K; Y. B' L! F7 H - dimLine3_endPoint.HelpPoint.Z = 0.0;
0 A$ Z6 Q5 Y0 \, d: e6 P7 P+ s - dimLine3_endPoint.View = nullNXObject;
6 F) r, c) J, h/ q( P
! f. T% H( ?+ u3 l7 M- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);6 ~5 k8 B5 k/ l7 V/ ^3 k' \
- 9 b" }) G5 ~1 u) j- [4 o
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);+ U- Y% l. I* {1 ?' y
- $ e7 `& ~: |! q' q$ D
- 6 {1 F+ T% w6 C
- " P3 T. n4 Z# ?7 ^
- //5 update & deactive8 v+ R7 L1 T7 w) ` m2 ~
- sketch1->Update();- ]$ y, y3 c& Z6 ] ?! w
- 5 _4 s! x/ A, v* H) T8 C2 O
- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);' A* S! g0 v2 i. A0 W! f, G" R; |
& J+ V& g" f+ w- delete expression1;
( i! _( [) S$ @! `( X: @ - delete expression2;" o7 `# }( m+ e; P# B
- delete expression3;
& l; {" t. y5 @ -
5 Q# X( p: ]" a' m& O( l# n2 L - }
8 Y, M* K$ ?; x/ d. K: |0 g
复制代码
8 x1 C+ {2 \. x& [' E) Q+ ?5 k5 \
* G* t( @' p- }+ T1 B
8 c* V( @* P! }
# R* G8 s, ?, A/ g4 E9 U) g |
|