|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建3 w: H. Z6 R2 V
内容:$ u# }% j. H7 b0 T- v
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。( D# |- R. I# I9 U' i# g: |+ a
目的:5 R& n% ^. C; [& E. h
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
3 @- T% J4 c6 D# w: T5 [0 H
NXOpen C++创建草图基本特征
% O+ A7 u" u9 U0 H1 z6 |' Y+ d
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍
( l$ I$ b5 Q0 r0 V. Q' ~2 r" n 31 NXOpen C++创建基本特征-草图的创建(2)UI设计; P/ g: x' Z4 o2 M5 j
31 NXOpen C++创建基本特征-草图的创建(3)项目创建
4 `! y0 m0 s4 G; p 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建9 T" Y M# `* _& E5 M) Z! T+ @
31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图
. ^+ q. r" L: Z0 n3 u 31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束! u% e. P6 L: ]. a6 p. x' {! l
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束) ^4 ~7 j6 }1 y6 [. [( p* c8 g8 H
31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联+ A A. R6 f2 S; n
4 ?, |0 r9 H8 t0 _
% u( Q3 p9 Z5 u- f
& s; L1 i9 M P- {
R1 ]8 ~& N' j: o) q+ A, u0 e7 b0 r) A
关键代码分享:+ L' I# e2 z& U8 @
9 R* C: M9 C+ v) Y' i: N6 W5 ?( ~% g x% Y) C
- ! Z% ]$ e4 N) V
- void lesson31_createSkeTCh::createIPSketch(), v1 v9 b7 s7 H/ C' [
- {
0 N/ M, i+ U. M m/ V
$ U1 X( _8 ? t5 h- Sketch *nullSketch(NULL);) B" A! x2 b( x9 C; s
- SketchInPlaceBuilder *sketchInPlaceBuilder1;
; Y m7 O7 ?2 O: P% r: T8 J - sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);# M2 f8 E" ?! o1 c+ V
- sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);9 j) v k) h! k. Q7 {, e
; A1 z4 H# }5 W) I9 V- F- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);% U' I( O$ d# j. V0 Y* ^
- 1 B4 u# N& l. l/ i+ x- {) {
- % Y ?6 q$ o0 R
- // add preference
; j K) y7 J$ T - & R2 w! ]" L- A* W
- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);7 C! q( F, A4 c
9 ~, [3 ~# C" b% O7 M; J- Drafting::PreferencesBuilder *preferencesBuilder1;
$ |7 u }" l7 P6 `& \ - preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();
5 V# N' f9 y4 \ - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);
4 B7 X/ D/ p, C; f" ^) S& I8 j/ ` - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);9 z. z6 O) v) x6 k
- preferencesBuilder1->Commit();
' S: G/ U. X, d0 L+ v9 J
' g! P6 r. T+ i# u1 Y7 _- preferencesBuilder1->Destroy();% A/ N; F4 w! P& o5 @" C6 w
- : j8 e- d* a( a9 g4 V2 q
: {& d6 [) v8 Q% _- NXObject *nXObject1;+ I* G* u' ?+ {# {: W- S% \
- nXObject1 = sketchInPlaceBuilder1->Commit();
) t. T, M/ H2 t4 ^0 k9 O0 H - * {9 V' Z! O! b/ ]9 Q
- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));- x, E1 j9 V g, U2 I; ?
- Features::Feature *feature1;; {( Z! K M% q! I. A& u% E
- feature1 = sketch1->Feature();
$ S2 |5 \2 ?- i% s1 Y5 i/ ?7 u - feature1->SetName("XY-Plane-Sketch");& |2 R h+ B% A2 p
- " T& u: w/ Q1 U1 l8 n. z" p5 P. {/ Z
- sketchInPlaceBuilder1->Destroy();% M9 U% c. e" i" i
- y4 x. @* X) m S* V4 J8 u9 q- //1 active sketch
) Z5 P# I: m1 e" N5 a' H4 l
/ ]" A" f* W4 V; B9 ^/ ~- sketch1->Activate(Sketch::ViewReorientFalse);' N2 w+ |& n2 r8 L. o2 W, X$ L
-
! t1 n; J+ Q) G4 K, K* j - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
* u. I# Y4 ?+ f+ k" G% a$ d - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");
S6 L* r" l! R" i+ z$ b - double length = this->expressionLength->GetProperties()->GetDouble("Value");
- D- j1 j+ x. l, U5 T. f# Y3 M# p - // 2.1 create geometry% f- s( S8 ^ u3 y1 } L$ g+ ^& I
- / o5 D7 P$ Z, [ T% m; W3 c
- Point3d startPoint1(0.0, 0.0, 0.0);2 z# c4 Y6 r8 l, ~! I
- Point3d endPoint1(0, height1, 0.0);' e* S! j% [; j" w q% k) E$ w L
- Point3d endPoint2(length, height1, 0.0);% Q" z4 D2 H; n( P' U1 P$ A
- Point3d endPoint3(length, height1-height2, 0.0);
. f0 T/ B, c/ ]0 x6 e- W- D; O" j - Point3d origin = sketch1->Origin();
+ }) J7 z5 t' i5 s U6 I4 j2 B9 g; m - Point *originPoint = workPart->Points()->CreatePoint(origin);
/ S8 C. t9 y. ~3 G1 q - % I9 }1 u5 \4 i
- Line *line1,*line2,*line3,*line4;4 w5 }; U1 n4 U: e
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);0 X9 O% G- ^" [ h, T
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);
5 l- l9 U, I- _+ q+ K, l+ u$ M' n* g - line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);
& I! U- D ^/ |3 N+ }+ Y% ]) ^9 _ - line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
* N9 P; S4 S1 U2 x - // 2.2 add geometry- i) [7 s3 d7 i( I0 h1 `; p
- ) ?3 y% w# R$ _8 a" n5 D
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
. {: _; m6 `- v - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);7 m1 Z+ X% N7 j; c! D
- sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);# }* s H9 A2 b, i% q* g* j. V( r4 }
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints); G2 ] V% d* n; @6 g! X; w7 B
& ^7 \' s- P8 K+ f% y! n) f# g- //3 add constraints ( gemo)% R y$ ?# A/ O! i" ^9 U) P% b
- 3 G- ~" E$ F1 i4 o$ b
- Sketch::ConstraintGeometry geomline1;4 u B8 k* _" C' V
- geomline1.Geometry = line1;
( [0 h# Y( M$ l- M - geomline1.PointType = Sketch::ConstraintPointTypeNone;& J# L7 b# L: N8 } q
- geomline1.SplineDefiningPointIndex = 0;
3 L6 T1 v5 ]0 V7 }) a. f - sketch1->CreateVerticalConstraint(geomline1);
; ~' t# @3 J( e: W1 p - , H/ e8 v6 @+ H! v/ y
- Sketch::ConstraintGeometry geomline2;
/ v. y4 d1 B3 f0 y+ H- Q# Y - geomline2.Geometry = line2;% D3 r' z: O# f
- geomline2.PointType = Sketch::ConstraintPointTypeNone;
* l- A7 F2 U( Q5 ^+ y - geomline2.SplineDefiningPointIndex = 0;
7 r. f5 w( f# U6 @9 I5 I - sketch1->CreateHorizontalConstraint(geomline2);
+ Q; a% M3 }0 E( k" R" U - $ Q7 Z3 T! s8 J* N
- Sketch::ConstraintGeometry geomline3;* F. p% _+ H! l0 P
- geomline3.Geometry = line3;! D) \/ ^& v: |2 T8 G, S1 H, L9 T
- geomline3.PointType = Sketch::ConstraintPointTypeNone;, ]) `: L, g, \) G
- geomline3.SplineDefiningPointIndex = 0;2 R$ c& L" w I7 J# r W& Z
- sketch1->CreateVerticalConstraint(geomline3);4 g& z7 p# h& ~) t- j2 r, G3 R& ~
6 Z* l c, m3 o) }; B. W3 e3 X- Sketch::ConstraintGeometry geomline1_startPoint;7 u6 ` d% R9 y, v j! a5 O3 a+ Y
- geomline1_startPoint.Geometry = line1;& ~# N0 g6 d9 Z1 U- ^; y5 o
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;% r4 B* D* A2 z g5 ]
- geomline1.SplineDefiningPointIndex = 0;
T: Y7 Z* n- P6 n3 q - Sketch::ConstraintGeometry geomline2_originPoint;
' S. S0 A) i$ U - geomline2_originPoint.Geometry = originPoint;& s- X% X; y u; n: Z
- geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
; x' v9 `: I$ D - geomline2_originPoint.SplineDefiningPointIndex = 0;% M& d2 ]# m7 p; o5 d+ ]3 d n- j6 s
- sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);
- z) U! E/ z# Z% p$ ^" p: m
- L/ d+ \$ g' a2 _5 l- //4 add constraints (dimension)
7 V5 g# p; ^0 z/ l& z' I, E
9 a/ X3 x& w8 s8 M- NXObject *nullNXObject(NULL);, F4 C( C2 d/ F# g# \
- I( p9 i& Y. ^' k1 n! F1 r
7 C5 J( S! A2 w. m0 X: x7 }- stringstream s_height1,s_height2,s_length;
/ I7 {- i- m. E7 N - s_height1 <<height1;
3 ]) ~* @8 r* f- q% _- c - s_height2 <<height2;
1 ~+ E- v+ {$ z5 ?0 l! u- M, m# ]9 r - s_length <<length;9 v( P/ l" m" m0 o
# F4 p* E; t6 G% o: |- Expression *expression1,*expression2,*expression3;
' W4 Q! m& K: U$ X- d: c - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());) u, y' f0 T6 N$ W& E' y& {
- expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());
0 C8 Z" y, `( X/ r$ d8 r* a - expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());
0 x; y1 Y% j; E- C
5 r# j A5 z+ @$ W' _5 h ]- // line1 dimension # i1 s- s% u5 a0 y6 e$ T
- . t4 O" `! q1 G0 N9 g
- Sketch::DimensionGeometry dimLine1_startPoint;
0 e+ n0 K. q% W4 {1 i - dimLine1_startPoint.Geometry = line1;
/ c, |" k, K$ E - dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;2 @% F( l; ] |4 Y* F
- dimLine1_startPoint.AssocValue = 0;
2 Z1 S( j1 K4 Q! N7 D, ^9 P8 T, k - dimLine1_startPoint.HelpPoint.X = 0.0;
* a4 v8 l0 S3 k, n; [" s( [ - dimLine1_startPoint.HelpPoint.Y = 0.0;
) ^& ]. F r; M' \& G" K - dimLine1_startPoint.HelpPoint.Z = 0.0;5 c- M1 h4 E W$ S. T4 T3 z$ A. O. r) Y' X
- dimLine1_startPoint.View = nullNXObject;! t( N) T- A) F9 m* Y
- + P; W! n7 O; |
- Sketch::DimensionGeometry dimLine1_endPoint;
, C& t4 V0 V* u e: x - dimLine1_endPoint.Geometry = line1;0 `& t3 l$ U3 f! @; f' {
- dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;
! F9 R# p8 C9 U- B& v. N, B6 K0 J( A - dimLine1_endPoint.AssocValue = 0;/ B' ^& S- w5 a5 c* y
- dimLine1_endPoint.HelpPoint.X = 0.0;) P' p* s) j, d5 M" j3 ?
- dimLine1_endPoint.HelpPoint.Y = 0.0;4 f! L& N/ [" B! Z& z- |8 m
- dimLine1_endPoint.HelpPoint.Z = 0.0;( C! x9 k7 S- V& l7 C
- dimLine1_endPoint.View = nullNXObject;- R8 ]5 H( s6 [8 _ E$ z
- ; A' ^1 w8 T# ]
- Point3d dimOrigin1(-100, height1/2, 0.0);
) ?; P9 _$ N+ V# k/ ^( D
! ?# ]4 H* j1 K- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
7 M/ w0 q4 Q7 q- O/ @1 ~) Y4 X+ w - 1 f5 ?" p( t: m9 R
- / r) a$ P, b; @
-
j9 D3 c3 _, d& n8 f* c - // line2 dimension
! T6 G5 p/ [8 L- [, ]2 t) u - ; k/ f( H! {3 U: C, k7 M
- Sketch::DimensionGeometry dimLine2_startPoint;( o+ P, v- I* y( M% ^3 S' o; e
- dimLine2_startPoint.Geometry = line2;
/ X1 h8 i5 q3 l; M2 n - dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;
! U0 t4 M& D! j4 s* Y8 U- v - dimLine2_startPoint.AssocValue = 0;/ V, X# D& K! r9 b' s$ E& x
- dimLine2_startPoint.HelpPoint.X = 0.0;' q& e$ }" e! z0 J. T, K8 H# w$ M
- dimLine2_startPoint.HelpPoint.Y = 0.0;! J7 Z9 n& z' M5 K' p2 v' H
- dimLine2_startPoint.HelpPoint.Z = 0.0;
) f) i+ c/ Q4 m' K - dimLine2_startPoint.View = nullNXObject;
& l4 B' [+ X1 z& {2 w - 8 @( u1 p6 v; c' a$ ]
- Sketch::DimensionGeometry dimLine2_endPoint;' x/ b! j5 ]! l
- dimLine2_endPoint.Geometry = line2;
; [) j7 k+ L; U - dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;) D% a3 c2 R" \0 e/ E' J; Q3 \' @
- dimLine2_endPoint.AssocValue = 0;6 b" F5 Y# n- a5 O! J) V2 ~9 h1 g
- dimLine2_endPoint.HelpPoint.X = 0.0;% p) a# L2 _1 p, x- r8 i# F5 f
- dimLine2_endPoint.HelpPoint.Y = 0.0;; Q7 j C2 h: W- s
- dimLine2_endPoint.HelpPoint.Z = 0.0;
( a s/ c& G- L9 i) g - dimLine2_endPoint.View = nullNXObject;
5 A r" ~1 R' K8 L! v. f - . W* _8 ^( ?& f* h( \
- Point3d dimOrigin2(length /2,100 + height1, 0.0);$ @* C9 R- \# Z8 |6 H2 a
- . [5 p* g3 v6 |& }9 s
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);, \# u `* S6 P5 D# h
, U2 ]$ o* \0 x, ~# ~$ N- X
: c1 |" @4 B& @. D4 O! p, ?2 Z-
# {" J b6 [; V: ?! ]6 o* y6 {0 o$ W - // line3 dimension % _' N# D; J5 J1 W" d6 i
- 2 r& i' b4 g3 h! h. E
- Sketch::DimensionGeometry dimLine3_startPoint;
7 @5 H/ P* T# |! ^$ B$ ? - dimLine3_startPoint.Geometry = line3;
& \7 Y- R' h3 m( a8 U - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;
+ u7 P6 e$ t& H/ O" a, a - dimLine3_startPoint.AssocValue = 0;
; Z; t) G4 J* ]- r4 E - dimLine3_startPoint.HelpPoint.X = 0.0;2 f0 X/ ~* }* U" t4 v5 h# k; |
- dimLine3_startPoint.HelpPoint.Y = 0.0;
) b7 I6 f( ?: ^# g9 ~ - dimLine3_startPoint.HelpPoint.Z = 0.0;
7 Z0 K! w( U7 D+ u! X. ?7 b! h7 i - dimLine3_startPoint.View = nullNXObject;
1 w9 I3 l6 ?7 H
5 f8 N% j5 j( q6 I5 Y$ e" x6 V- Sketch::DimensionGeometry dimLine3_endPoint;; Z: [5 Y- v. }# \" e: y
- dimLine3_endPoint.Geometry = line3;
/ e4 v; }: a- v) I/ z& k6 H- k" @ - dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
( M' F( I( ~" y* r - dimLine3_endPoint.AssocValue = 0;) |) q9 J5 w4 L0 n& L
- dimLine3_endPoint.HelpPoint.X = 0.0;1 a0 E, \& U. C4 v% E
- dimLine3_endPoint.HelpPoint.Y = 0.0;
7 |$ _! _3 |: h6 j" R - dimLine3_endPoint.HelpPoint.Z = 0.0;$ O, ^( ~5 f+ X3 Z Q. ]6 Z9 q
- dimLine3_endPoint.View = nullNXObject;
) P2 B6 H+ P8 ~" U; N
4 q7 d- T; \( n* A8 o6 p% L- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);
' p* @4 M8 w3 e* s( Z - 4 c$ w* I3 l8 {1 V4 A t2 q/ r
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);
* C$ m# ~" J( d0 ~
) \' U! _' ~. w7 o( }0 P5 Q
2 f# ]% ?# L5 L7 j- + ^8 B7 C+ e. K- ~ f+ ~
- //5 update & deactive$ e; U; r4 f3 }2 K
- sketch1->Update();7 K8 ]0 ^6 Z% ]: G- _4 c$ Q- p
- - T: ?+ w9 ?& @1 {- y1 g2 f- I
- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);/ ~/ a% H8 c1 A- _ q) K: |1 Y, ?/ ]
1 s9 n/ h7 D& X: F( O: V, Y- delete expression1;
, m x1 W1 |( C4 o# u: C! U - delete expression2;
6 S# k- n# w4 Q H: [ - delete expression3;7 \7 H1 g6 _; |' j: V* i& {
- G$ ^5 ^) I q! \
- }
1 w: l( V1 Y8 ]3 x5 K! u
复制代码
) l3 M. d# s, d' P+ P5 z
$ o* Z' z' j2 O& h; g
( W6 [, R" n$ l/ Q+ I+ A
) o" V0 c x# t1 l/ M: F; j |
|