|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
& ^; E% H; B5 U& d0 L8 R" Q6 l7 v内容:
6 M, t3 E& O- Z/ Y, [4 M草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。7 `' W/ s( k5 T+ v$ [
目的:2 \; L4 ]6 p' U+ Z6 n: S, y
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
4 k+ T4 x$ |- e" \, [9 \9 f. s: ?
NXOpen C++创建草图基本特征
* k; `, D: o# r5 F4 X9 Y! j
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍
. f! A) V# M, o2 v 31 NXOpen C++创建基本特征-草图的创建(2)UI设计
# C) B, e6 a; H9 d3 b5 v- W* v% m 31 NXOpen C++创建基本特征-草图的创建(3)项目创建% l' ~7 q4 B( a( A
31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建
* R2 D( E# ], _# {3 | d" r 31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图
$ i6 j4 l" w# M" _3 G5 ` 31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束
# P: M4 S) \, ^9 h 31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束; s* Z! ^6 `- K4 \+ y2 }* G
31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联, K4 s* P4 \, w1 @
% V1 x& ~" D9 v& J1 H. j* u$ P
, ?3 S" j2 f' j2 c. V
7 {7 `% \7 m; N2 v9 W: N9 |5 M! `4 W0 f, W
- e! F2 ~1 {( N8 z+ D0 z
关键代码分享:5 I) ?% l+ F' U( [' h6 j' Q% Z
; k$ k- O$ }$ n+ R
$ w# Y5 H2 n i( F: x9 w- ) J! o+ Q5 |3 M8 _# W+ C* B
- void lesson31_createSkeTCh::createIPSketch()
% T5 z% Q' Z$ \- s. f - {
8 c; V, ?4 a% w
/ d3 I1 }/ u: g" U9 l- Sketch *nullSketch(NULL);
. Y% E, p4 f0 c C$ z9 g' c' \ - SketchInPlaceBuilder *sketchInPlaceBuilder1;) r' b1 b7 k( j8 i" S8 W. s+ c1 F
- sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);) z8 L" n6 q$ l$ t; {, P- c. {
- sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane); }" o' O+ K! R: T6 @
) S9 |% J+ d- E) X# O! O3 u" K* F! [- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);
# w) l( k u9 G& ^% O9 y
5 D. J! R. W# p( w7 A! W( v$ Z
! ^9 S1 d1 y: z# S5 M! B) j; B- // add preference
: J% Q% ~- G0 q; ^8 a" p; D' @ - 6 C/ {( S& k' K' n' c8 C6 n
- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);1 i7 u. i B" U, i+ u" C
- 2 ^4 |8 M$ D: R2 ]: x# r& I2 w
- Drafting::PreferencesBuilder *preferencesBuilder1;- Z& r7 H" \, h6 U* k1 t
- preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();
6 ~. [) x6 l2 H$ Z$ u - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);, `7 r0 J* C- j# A
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
# h/ E# l+ e, N+ k, H ` - preferencesBuilder1->Commit();
% J7 m* a+ O* m2 F9 }$ e
( d r1 D, s0 i9 d& \- preferencesBuilder1->Destroy();
4 k! a n! ^! f9 f, [/ ]3 c9 G+ A - / {% J$ N9 O2 D
- 8 M' j$ T/ ]( I/ F. T
- NXObject *nXObject1;! U8 {; e$ R; E: w/ F. o
- nXObject1 = sketchInPlaceBuilder1->Commit();) F* e" Q3 ~% I% u; s0 S; E7 \+ @
- 2 c R. m/ p) ~
- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));
# e$ \/ {6 u1 @; v! p. J+ U) x# ^. b - Features::Feature *feature1;
, N# m/ W- m- l; [' g7 [ - feature1 = sketch1->Feature();
% K* X$ ^5 t m5 E2 [ - feature1->SetName("XY-Plane-Sketch");
3 u) T* D$ n. A2 \4 g( R -
8 j* D! H$ ~) w3 D7 p - sketchInPlaceBuilder1->Destroy();
- l( J) }! F& a* M( V9 d1 _( Z
% K# Z# y9 i) P' |) r/ G; l- //1 active sketch
! h+ @4 A, i2 \5 h! m: d9 b - + O! W' G- c. d1 t
- sketch1->Activate(Sketch::ViewReorientFalse);5 N7 V6 Z: Q5 I. ^* Y! Z3 h& Q: y4 j
-
$ T# [7 q0 t+ a7 v0 c% V: G - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
; |/ R9 p. l: Y0 z - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value"); O L- M- p& m d' L8 ^/ V- k! _
- double length = this->expressionLength->GetProperties()->GetDouble("Value");
7 U' B* Z% x) } - // 2.1 create geometry$ O5 [" p# x' [) p3 z% f% p+ @3 }
- . L- Z% f& J! V& W8 o. P
- Point3d startPoint1(0.0, 0.0, 0.0);
- A- y8 P. x% g - Point3d endPoint1(0, height1, 0.0);; b! F* g' _, ?# G
- Point3d endPoint2(length, height1, 0.0);" b. Y& j1 M, K& P5 D
- Point3d endPoint3(length, height1-height2, 0.0);; K3 T) A' N; `
- Point3d origin = sketch1->Origin();: u; e# c1 i: [. D' m) s( t' S* ]3 x( s
- Point *originPoint = workPart->Points()->CreatePoint(origin);- V. ]( g+ L$ Z3 V' }
- . _- S+ a" z8 E7 v& g/ t" s
- Line *line1,*line2,*line3,*line4;0 Y8 x& u: s( s
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);# B0 Y% r1 f( U! t; z9 q" ]
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2); r, Q; n. g2 k6 |, Z
- line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);. |7 W" c4 L7 F
- line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);* f/ X5 r m% ]8 S, c; a5 |
- // 2.2 add geometry# [- Q9 w L+ `
- $ u" t# Z8 x8 b( y& f
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
6 ^% Z* ^4 N0 S0 u; Q - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);
' O0 g2 Y/ e3 @$ K - sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);' O1 z( x8 z9 T6 r& O. Y8 c1 o$ m1 F% }0 w
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);
" J. L' X/ P9 k, s0 k - $ O. B) s+ R+ r V% v8 a, E" h1 S
- //3 add constraints ( gemo)' b2 W' @* a6 ^ q
- / w3 i1 V+ T; ^% d% p7 t
- Sketch::ConstraintGeometry geomline1;
6 k) B8 c# Q$ f - geomline1.Geometry = line1;
! G" m; v2 q' H0 C - geomline1.PointType = Sketch::ConstraintPointTypeNone;/ E6 I. F/ l: U. N5 c, q
- geomline1.SplineDefiningPointIndex = 0;0 m+ n4 B5 \+ [+ N1 ~; D: T
- sketch1->CreateVerticalConstraint(geomline1);/ P5 O- W; U! v& U- L& B6 P+ @
2 d: w" h: k- j S8 n- Sketch::ConstraintGeometry geomline2;# B+ I. [1 z5 |2 p
- geomline2.Geometry = line2;$ H/ l- Z( N! }+ I
- geomline2.PointType = Sketch::ConstraintPointTypeNone;8 Q0 B" Z8 L! R& r. H
- geomline2.SplineDefiningPointIndex = 0;3 I7 j* C# ?! c7 D
- sketch1->CreateHorizontalConstraint(geomline2);8 O0 g- h/ E: X5 V8 U
+ e- H3 W. ~4 x/ p3 u2 E& o- Sketch::ConstraintGeometry geomline3;" S3 n" q9 @/ u# E
- geomline3.Geometry = line3;
; R& ~! z y. z" D% M - geomline3.PointType = Sketch::ConstraintPointTypeNone;& t; m/ r9 o$ _, h* R- R
- geomline3.SplineDefiningPointIndex = 0;
$ m9 T3 S, `" i w% d - sketch1->CreateVerticalConstraint(geomline3);
8 B% ^0 \# z1 F c. m - # ^! I5 b, a7 R' {. P( G( p- `
- Sketch::ConstraintGeometry geomline1_startPoint;. k r' j* F" p+ ~1 `* T
- geomline1_startPoint.Geometry = line1;$ \3 c0 N$ Z/ m5 x
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;( [5 M4 R1 g/ {' q% z3 y5 t
- geomline1.SplineDefiningPointIndex = 0;, }2 ~0 F* [ o0 _9 S2 V
- Sketch::ConstraintGeometry geomline2_originPoint;# _, t7 B2 d& H1 n
- geomline2_originPoint.Geometry = originPoint;4 J, @0 C9 u' D9 J" ]
- geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;) X/ Y- }7 c- l
- geomline2_originPoint.SplineDefiningPointIndex = 0;
& C+ M- C8 t; y9 i& l - sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);4 j" W* k* w5 a2 j/ A; h
% } R, a7 N ]0 F( g1 S; A, M$ l- //4 add constraints (dimension)
2 T+ u9 Y% R, E, n
& n5 e7 t* ~: Y% f( z- }) U6 ~7 h) `- NXObject *nullNXObject(NULL);
$ j: [# w& h# {4 D0 ] - 4 I* k% Y1 x# b8 K
, J+ x% S7 c* L, p1 L- stringstream s_height1,s_height2,s_length;
, F1 b2 z+ s0 U9 v - s_height1 <<height1;
; ~' g5 s5 c$ Z3 G2 s G - s_height2 <<height2;
" t; g+ F) ^' _- W6 k - s_length <<length;
: D: S+ O: B$ P0 d0 g- \) ]* p - 5 G. c; g |4 H8 b. g6 h2 }
- Expression *expression1,*expression2,*expression3;5 C; H2 v, o3 e: b; q- x
- expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
% U" v: o% [4 E1 F0 I - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());
5 R' m2 T2 V: j1 e - expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());
c9 l% P) ^" C L& R - 0 i4 S9 @1 K$ {# r" ~" N% P3 {
- // line1 dimension 2 z5 e9 g4 V! f& {8 r
8 T+ r8 i/ Y' y- Sketch::DimensionGeometry dimLine1_startPoint;+ S' f' t0 r( w% V) c, q# h2 B
- dimLine1_startPoint.Geometry = line1;5 R, ], F2 i# h; P
- dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;9 Q p, b6 I/ g
- dimLine1_startPoint.AssocValue = 0;+ ]" z+ x* ^8 S2 L
- dimLine1_startPoint.HelpPoint.X = 0.0;. \ j: G* M6 s) o2 e5 r# o; e
- dimLine1_startPoint.HelpPoint.Y = 0.0;3 P$ \5 b0 v3 e9 X& B
- dimLine1_startPoint.HelpPoint.Z = 0.0;
: F$ @8 P# N# Z5 S' c: w9 Z! c, @ - dimLine1_startPoint.View = nullNXObject;/ l6 m$ }- N# z3 ^
% W- }( G( y: a- Sketch::DimensionGeometry dimLine1_endPoint;
+ o' @, Q( x- S - dimLine1_endPoint.Geometry = line1;, n+ Z" K0 g. M8 c
- dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;5 \5 r9 O" O7 L, Y* E$ c
- dimLine1_endPoint.AssocValue = 0;* e1 |" q) J! h% t: O& [8 L
- dimLine1_endPoint.HelpPoint.X = 0.0;7 N6 q/ P6 s1 j) e- ]
- dimLine1_endPoint.HelpPoint.Y = 0.0;. c; T' a3 t a( M% _
- dimLine1_endPoint.HelpPoint.Z = 0.0;
1 a, Z% `7 q1 x. u; R7 E) t( J - dimLine1_endPoint.View = nullNXObject;
, ~3 h3 a8 i$ C. H, b- {. O - ; \3 X# k: ~5 c& B4 ]
- Point3d dimOrigin1(-100, height1/2, 0.0);
9 Z$ I3 j- [7 D6 m' K - & H# H5 o4 {2 U, o" s
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
7 l$ l: {$ u9 E7 B) t
5 b: ^! s5 [+ b& a' L+ i& x
$ h( {' ~ {. P' ]' N F- " P+ W! U, _6 A% S
- // line2 dimension , q; u) V+ Q* F u- P0 j
. @/ r5 |6 |1 y1 Y7 P- Sketch::DimensionGeometry dimLine2_startPoint;. o) g* @6 K! j7 @+ x4 M3 L8 @
- dimLine2_startPoint.Geometry = line2;, i5 ~( {4 X: O; ^
- dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;$ E. D1 }" f: n3 \6 y
- dimLine2_startPoint.AssocValue = 0;$ h2 }3 E% t8 y3 n( q7 i
- dimLine2_startPoint.HelpPoint.X = 0.0;
* b: g1 i% `0 I5 ^0 W# v - dimLine2_startPoint.HelpPoint.Y = 0.0;
$ [% U) H9 f$ z3 d5 T% h* _/ L - dimLine2_startPoint.HelpPoint.Z = 0.0;
. S7 E0 x+ ~0 Y$ `* L! p - dimLine2_startPoint.View = nullNXObject;$ k6 _. V1 y0 V& D! G
! j; ~' p0 {# Y6 x( P- Sketch::DimensionGeometry dimLine2_endPoint;
V2 i: \0 I+ E: Q - dimLine2_endPoint.Geometry = line2;
a1 p3 ~6 Z% Z- E - dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;7 @/ E* T+ d4 S ]% O# ~2 n
- dimLine2_endPoint.AssocValue = 0;
5 N. r1 i5 ?2 c- @$ `2 U - dimLine2_endPoint.HelpPoint.X = 0.0;- R2 g- |0 S# U6 G" x8 R
- dimLine2_endPoint.HelpPoint.Y = 0.0;
' S9 n$ S. M9 m5 Z8 ~ - dimLine2_endPoint.HelpPoint.Z = 0.0;( F# ] Y0 p6 W! T! j" X0 I1 H/ p! L
- dimLine2_endPoint.View = nullNXObject;- O3 C1 I( r; {9 s3 y6 U8 d
- + E; J( o7 N, R5 w) [( Q& Y& |
- Point3d dimOrigin2(length /2,100 + height1, 0.0);
4 V3 K3 V8 Y' f' B; A5 a" x - ( O# W6 N% P( y4 w( Q
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);+ L0 V; e0 ]% X& k; d6 x
; ?- d7 h# s6 D: Q% R$ s+ y- * T0 s) _( K. A& g
- & e6 j" k m# L) y D7 s* Y
- // line3 dimension . E8 t+ ^2 o! D# v% t8 q
- % W3 \& o9 G/ T" g4 @
- Sketch::DimensionGeometry dimLine3_startPoint;3 j, M& x% l6 G7 l8 y M+ `# o, ^( v7 b
- dimLine3_startPoint.Geometry = line3;
6 `* M+ z2 B% b- h - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;% t t" V/ @0 K. `5 G; Z
- dimLine3_startPoint.AssocValue = 0;
& ?3 w' l/ D5 O$ ~9 j5 j' N2 v9 t - dimLine3_startPoint.HelpPoint.X = 0.0;
* l& i" V( D+ v - dimLine3_startPoint.HelpPoint.Y = 0.0;
3 t7 }% f# a, m7 F, W2 u - dimLine3_startPoint.HelpPoint.Z = 0.0;
; H5 A% V/ o K0 b* b: D2 G - dimLine3_startPoint.View = nullNXObject; p# c% ^# |1 e" X
& s6 q5 y' V4 q) P2 T- Sketch::DimensionGeometry dimLine3_endPoint;
. r( p( ~( L- Z' t( Z - dimLine3_endPoint.Geometry = line3;& B0 Y2 L5 l3 N0 N1 T) E' `. Q
- dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
5 J$ k0 G# Z+ v4 v. b - dimLine3_endPoint.AssocValue = 0;
& z# F8 o. e/ t$ S4 f - dimLine3_endPoint.HelpPoint.X = 0.0;
( m# c( Q- n: G3 ~2 N# z& R9 a - dimLine3_endPoint.HelpPoint.Y = 0.0;
4 W: e! Q$ H) \# I - dimLine3_endPoint.HelpPoint.Z = 0.0;8 f6 R; \1 K3 M+ X* t" i+ U
- dimLine3_endPoint.View = nullNXObject;9 K4 O( ^1 d+ U1 h* G5 Z& X
8 T; Z! H1 f( ?; n2 d1 o( r- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);
$ [) U' i0 h1 e1 S: \ -
6 Z4 t' N( d5 Z- }4 Q4 M - sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);; L$ G. B- M3 [5 O
7 ]1 }+ q" m: S- L x( Q
' R- Z2 `4 j9 t. ~$ S
/ @1 X8 }+ c) W# y4 V- //5 update & deactive7 I% f: R9 w4 `) r( C, M: F
- sketch1->Update();- K; G ~- x% x3 k5 V
2 g o1 w+ [9 _: ]( l- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);
3 o9 n0 s. x7 f2 V" _
' j- Y- r( |/ H6 Q4 Q- z7 ~) |# c- delete expression1;8 E; U& b4 S) t( N3 |' P
- delete expression2;3 b5 @& p& A# ?) S
- delete expression3;
4 m4 o7 Q) ]; ^1 }; ~: ~6 I - / M. j# C3 k8 r* g2 {
- }
2 v0 @% ^% I% j' ~
复制代码
: U) v$ a K7 _* J5 G& r: h/ |+ @& C" h1 r
: G- p# |2 S- a0 x+ C7 I* ~0 a1 h9 P
|
|