|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建
1 l. A& i4 h2 q/ Q& e5 j内容:
, a5 v7 z0 l: [( ^草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。( R* ]) Z8 |6 j
目的:
9 j- c$ a) G5 @ X1 s, H在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。
: f7 v2 ]% `" g3 l% q) l& G+ s
NXOpen C++创建草图基本特征
/ h+ m' v' }( D, W, l' u7 ?# `
31 NXOpen C++创建基本特征-草图的创建(1)项目介绍" o4 J4 W5 L. i2 f8 K
31 NXOpen C++创建基本特征-草图的创建(2)UI设计$ U# k6 ~. r- k* ]1 x3 t; l4 t8 u
31 NXOpen C++创建基本特征-草图的创建(3)项目创建
s: C4 Q% L8 D; q; k, r 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建3 k, y5 Y1 Y% R4 A8 Z
31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图
3 A) s; Y9 g6 d 31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束1 h( Z n1 j' F( R% K* ]. a; e
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束
1 L0 p4 b$ ^: g% t6 ^5 k2 a 31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联% w8 i6 G3 \- r s# F! A& x
' B# m" Q4 S# U0 I' p' J
. i- j, @* a* r
$ C% R! ^6 n* v I- ^& `' v
; G/ p' _8 v: G9 s8 t; [$ R9 \# j* E& u# m( C5 S0 I8 d
关键代码分享:
, y: C( Y& q* ?5 l0 f% v. v9 H( H8 l) h; h
f/ k6 P! G1 D9 u% w
- 7 w) }( P6 R0 k# d8 P- U1 I
- void lesson31_createSkeTCh::createIPSketch()
c4 a: S1 T+ y! N! f3 L& R - {
# G6 ~! w3 \4 i- \( J" ?) K' S5 p
J4 \3 h, |7 a7 i- Sketch *nullSketch(NULL);
8 l! c) @5 Y1 S7 g0 C - SketchInPlaceBuilder *sketchInPlaceBuilder1;
: _7 b U) Q3 J - sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);
0 o' }7 y3 A2 F0 \1 i - sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);
, b. h) D5 g) ]3 P - 1 k/ i6 i G. [! a7 F
- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);
! t* T+ a9 u& x2 z& x! R - 4 ]0 \+ G, A+ h) E% `* z1 G
- % x* _) Y! u8 a0 v, e# J
- // add preference
" U2 \/ D9 n1 ^4 {" F( l
3 e( N! ^4 c& a( l& T6 [0 z- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);
) M7 H! }* h9 {0 X7 r6 N3 B
* j+ J- P! j4 q1 E' O( I G" B/ u- Drafting::PreferencesBuilder *preferencesBuilder1;
3 v; w V3 s2 n8 W, ~ - preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();
8 N/ @/ B9 F* A: H: f% Q2 N L, \ - preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);$ o- Y( J) {1 i6 D
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
: Q* B* n4 d4 i$ T- w, y - preferencesBuilder1->Commit();, o- O8 A& L/ e X! S
- - o/ r. _0 M; @
- preferencesBuilder1->Destroy();$ L3 y" R/ k8 P+ i0 L: H4 f, D
' d) ]9 x) ^0 S4 R
7 G5 |, h& {( P1 `- w+ W8 [$ t1 |! `- NXObject *nXObject1; N7 _! J# r7 P. c
- nXObject1 = sketchInPlaceBuilder1->Commit();2 f& e" ~. E2 v7 ?
- - }; B) Q" f: k5 @4 A0 `$ j
- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));
! _. O5 i' B3 M2 H& G - Features::Feature *feature1;
9 G- s4 a$ d* {# j/ W - feature1 = sketch1->Feature();. h& q: V3 D, v* v; s
- feature1->SetName("XY-Plane-Sketch");
- S( U3 Q4 k' ^ - ' M9 Y7 a# {, y9 Q9 t
- sketchInPlaceBuilder1->Destroy();
) _# V/ ^1 V9 @% A7 Q. _* ~
) {4 Z' ` @' N( N- //1 active sketch $ B5 R1 R& G. P0 l5 ?8 W, P; a
- $ D, L2 R' X' t6 I+ K+ R7 _! N
- sketch1->Activate(Sketch::ViewReorientFalse);* @ [8 E4 Q" A3 @4 n
-
0 q. N+ Y4 ^* S0 F1 b1 N: ` - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
" g$ ^/ d9 U" o2 P5 I% ^9 n; [ - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value");
9 `$ S: v' v7 k9 ^) T; @# @ - double length = this->expressionLength->GetProperties()->GetDouble("Value");# a1 K3 x! V* C8 N, ]
- // 2.1 create geometry* k& q7 X6 J" ?# s( Z
- c" n" t1 t; {% u! F& q U- Point3d startPoint1(0.0, 0.0, 0.0);5 h* V, A% ?; f! R" f% {; m& g4 [
- Point3d endPoint1(0, height1, 0.0);3 X( a+ F1 x6 N+ g- `2 U
- Point3d endPoint2(length, height1, 0.0);
+ L6 G% f; Q$ c/ _ - Point3d endPoint3(length, height1-height2, 0.0);$ c: O( f' G- h y8 u- u4 w/ k5 R# J
- Point3d origin = sketch1->Origin();
4 ]' @! @ H% ^8 T5 ]8 D, p+ n' J/ y - Point *originPoint = workPart->Points()->CreatePoint(origin);
) B! B A2 v: i
! W8 q& l6 p8 x" C4 A( f) q: Z) G- Line *line1,*line2,*line3,*line4;4 {8 E+ A! G6 L0 Z* A
- line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);" @. F- I: M! r, u! k
- line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);3 S, f5 m7 W: R3 _
- line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);' X* @7 X) C* \3 `, {1 m; B
- line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
' Q7 n) V: u8 Q3 W( f7 h- p/ ?2 k - // 2.2 add geometry- r) K, x; [4 o4 a& I* ]% g
- s0 f6 o; n! Q Q- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);
" u5 s. k6 E$ R) E( D - sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);+ T8 \* ~- x8 y# N
- sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);# b) I, b+ U) i+ J, V6 x) }
- sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);- H. V, N) u4 ^8 Z0 U
- 6 O/ m3 Q3 n) F* b9 f( u2 z: e
- //3 add constraints ( gemo)
& V _6 u3 ]9 A/ ]
x7 @* n) y* }/ y- Sketch::ConstraintGeometry geomline1;. e) E" ?/ z9 C% u5 g
- geomline1.Geometry = line1;
0 S& i& t$ t: V - geomline1.PointType = Sketch::ConstraintPointTypeNone;- L# Z8 D/ M0 ~, S R# X. R q9 k
- geomline1.SplineDefiningPointIndex = 0;
$ C. U- D1 E+ y4 h - sketch1->CreateVerticalConstraint(geomline1);
+ b+ Q7 [7 z1 w/ q; d - 0 F* ~8 k* Y% H7 z4 V, e' P
- Sketch::ConstraintGeometry geomline2;
! p, z- b4 r. |$ F) I - geomline2.Geometry = line2;9 S$ o- H2 P2 A0 `
- geomline2.PointType = Sketch::ConstraintPointTypeNone;
. s$ f0 U: t5 r - geomline2.SplineDefiningPointIndex = 0;, \1 ]4 o% w! L4 u8 v4 t
- sketch1->CreateHorizontalConstraint(geomline2);
3 h8 Q4 B2 X) \1 v
) F5 m: a' C7 F h! e, |# q- Sketch::ConstraintGeometry geomline3;+ N7 x( j* d* K. C% ^. c! f- Z4 {
- geomline3.Geometry = line3;! A- `5 y4 @/ S, w0 @" `5 W1 v
- geomline3.PointType = Sketch::ConstraintPointTypeNone;
9 F4 |3 `( o. n8 Y" n! P - geomline3.SplineDefiningPointIndex = 0;$ r# s( Z O9 T" F
- sketch1->CreateVerticalConstraint(geomline3);
! B5 k- W K) R - 8 U- N& b, [! }& j' u1 A
- Sketch::ConstraintGeometry geomline1_startPoint;6 Q7 u+ R* F! R0 D
- geomline1_startPoint.Geometry = line1;5 I- b$ `: T; v1 a- _' ^' _7 G. n
- geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;/ |9 H2 X/ g5 }1 b2 G8 j6 Z$ k
- geomline1.SplineDefiningPointIndex = 0;6 ~" t; s: X4 A" |6 @
- Sketch::ConstraintGeometry geomline2_originPoint;
$ Q' N @% C ]1 {; Z7 v - geomline2_originPoint.Geometry = originPoint;
+ J! H, o/ m4 @) Z" s! _. n! [ - geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;
/ c( g% F1 V; o- B$ } - geomline2_originPoint.SplineDefiningPointIndex = 0;
U4 I: m; S3 g4 t. i - sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);
0 t8 T2 g& I3 |4 { - # t6 M: H% ]. I, @* \- L
- //4 add constraints (dimension)- u' `4 p4 o0 b3 S" I8 P
`6 J3 S& F4 h: a. G4 Y) V3 L- NXObject *nullNXObject(NULL);
' o4 K+ W5 X0 u }( E/ ?3 O - 3 }# `( J' |; A5 i
- 5 o0 Z( a! Z3 R( F% {/ ~5 u
- stringstream s_height1,s_height2,s_length;( z ~0 A/ t7 f4 S1 m0 Y4 c
- s_height1 <<height1;8 b3 I& P$ p# [
- s_height2 <<height2;
. h/ x& \+ b/ S* r" X% T4 l4 L5 ] - s_length <<length;
2 s0 U! C) p5 [* Y% G - 1 S1 m. l* E" n& F% x
- Expression *expression1,*expression2,*expression3;2 M# }7 b4 o$ ~
- expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());
* S! v* x$ S$ ^ - expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());, I2 y% E) q5 y3 H3 S1 ~
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());- l; ~7 B/ v! g1 B9 K# T
, M1 G5 s8 J/ [- // line1 dimension ' W) g5 S2 T$ U7 _6 p
- ) r6 S* N4 ?2 ^# [5 Y6 W3 r4 G
- Sketch::DimensionGeometry dimLine1_startPoint;
5 n, Y4 j. v5 @+ k/ y/ o* ~ - dimLine1_startPoint.Geometry = line1;
& O4 o2 H! V, h% M) W7 e' \# y: g - dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;; Q) O) K. d. u# ~, T5 N" _
- dimLine1_startPoint.AssocValue = 0;" _- X: p c4 m- J) L
- dimLine1_startPoint.HelpPoint.X = 0.0;& T9 d! Z/ q. I7 x" |3 I
- dimLine1_startPoint.HelpPoint.Y = 0.0;/ ^9 ~& [) B$ a; T
- dimLine1_startPoint.HelpPoint.Z = 0.0;1 p H, ?% t' \9 N% v3 M
- dimLine1_startPoint.View = nullNXObject;
8 h$ L4 B% g+ X2 T/ m
5 g9 ]/ Z( l e& Y$ o- ~' e- Sketch::DimensionGeometry dimLine1_endPoint;( Q% l% V2 e5 i8 P- u
- dimLine1_endPoint.Geometry = line1;0 \: E, [7 }0 J" p# ^
- dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;: H' ~; O7 s$ I
- dimLine1_endPoint.AssocValue = 0;" m/ P( M4 @3 f2 V1 F$ r
- dimLine1_endPoint.HelpPoint.X = 0.0;
! A) G! g7 z* P/ w$ E' N - dimLine1_endPoint.HelpPoint.Y = 0.0;
/ M: K& h. m- ` o - dimLine1_endPoint.HelpPoint.Z = 0.0;$ Z0 q8 [4 ?* p1 t
- dimLine1_endPoint.View = nullNXObject;" l6 m) m- n0 n( G# X" Z& x
- " L2 _$ q' M# v% T! Z
- Point3d dimOrigin1(-100, height1/2, 0.0);
" }. J; K1 n/ Q' c$ P/ h D - 1 _. I1 |' j" t
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
/ E0 E& ?9 }' V* N& ^0 K - [: I) Y; C& t5 Y, [$ H
" P" @6 X' `- S( s( O- ) Z* I1 v; [$ t2 b; s3 T( U5 P
- // line2 dimension
: O! W6 M9 U- k( K1 W
5 [" N7 [" H/ ]! W! ?: a- Sketch::DimensionGeometry dimLine2_startPoint;. o+ p& h2 ~* g* X# O1 |$ T
- dimLine2_startPoint.Geometry = line2;- T/ I2 N9 n& a" A" n; o/ l
- dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;! Q2 Y# g+ N' m% p$ _1 D, {' z
- dimLine2_startPoint.AssocValue = 0;
% w* K# E+ m [0 Q4 T5 u - dimLine2_startPoint.HelpPoint.X = 0.0;6 P% k+ T( b: B# N1 K
- dimLine2_startPoint.HelpPoint.Y = 0.0;
8 [% B E& w) l; R8 Z* | - dimLine2_startPoint.HelpPoint.Z = 0.0;
5 I& m0 i9 v4 c7 ]$ S - dimLine2_startPoint.View = nullNXObject;
- W. p" a8 t; _7 i1 z7 y; G2 h k - 5 b s5 O6 r5 ?& d9 S3 [
- Sketch::DimensionGeometry dimLine2_endPoint;" Z, E5 M1 b4 s3 p- G
- dimLine2_endPoint.Geometry = line2;
# n7 i( V+ Y. x - dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;
4 F1 b- G+ \1 ` - dimLine2_endPoint.AssocValue = 0;5 t& c7 |4 R1 u5 g( a& R
- dimLine2_endPoint.HelpPoint.X = 0.0;2 A) ?! P) o: p
- dimLine2_endPoint.HelpPoint.Y = 0.0;
$ U$ c% j/ j. w - dimLine2_endPoint.HelpPoint.Z = 0.0;7 {" D1 I5 a& V% }$ t$ p
- dimLine2_endPoint.View = nullNXObject;
( ~2 t3 K- u9 n' s
_0 A$ a0 H3 O4 ?- Point3d dimOrigin2(length /2,100 + height1, 0.0);
9 l; C6 Y2 C$ x3 H; F* j
5 {. J7 u5 d! `6 a- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);
" w5 f& H7 F$ x7 V5 a - . J# a/ P" f% x. F
9 o. n! j4 D; H* Y7 b+ g- ) k! h4 V8 `( ~. l, T- t
- // line3 dimension
, g4 U/ T6 X: `9 y; z) E - ( D; i* F# _ @8 F6 q$ q, G
- Sketch::DimensionGeometry dimLine3_startPoint;
* p+ C) X% S6 h2 D3 q( K - dimLine3_startPoint.Geometry = line3;, P* o8 K3 x( r4 L: \
- dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;
4 t- \. ^" P* {" X1 I1 V - dimLine3_startPoint.AssocValue = 0;
* |. S: a. J Q - dimLine3_startPoint.HelpPoint.X = 0.0;& |" F- `: S' u2 Z" j- u% B6 l" v
- dimLine3_startPoint.HelpPoint.Y = 0.0;
* t* @- x/ O- f, P - dimLine3_startPoint.HelpPoint.Z = 0.0;
6 G4 z7 o B2 x, Q - dimLine3_startPoint.View = nullNXObject;5 B" g! t. a; W" Q7 B
- 9 s7 \$ v7 M7 v- k1 b$ G
- Sketch::DimensionGeometry dimLine3_endPoint;& t8 O; u+ o+ W" E7 g' g# C$ a4 [9 q
- dimLine3_endPoint.Geometry = line3;
0 e8 K" W- V. R2 F4 v - dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
3 ^7 p! Y2 [: l/ i! ] - dimLine3_endPoint.AssocValue = 0;) ^" g, ]+ O+ E4 y: U0 K
- dimLine3_endPoint.HelpPoint.X = 0.0;6 n' @2 n- ]/ M3 q5 ?9 X# H
- dimLine3_endPoint.HelpPoint.Y = 0.0;) k$ p% p# _; _7 n# b! O; h
- dimLine3_endPoint.HelpPoint.Z = 0.0;$ y* f. h4 g- Q. C( d1 b2 m
- dimLine3_endPoint.View = nullNXObject;
/ K/ D! i7 \) v8 B
5 l- {- S2 m/ _; B' F: x- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);
8 s% e) |7 D5 X% k - . J3 H; c9 X. B: A. x
- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);
6 w5 Q# d0 p5 K3 A3 j - 3 g; |" j, n# \ ^% \
- % k8 P5 y' P, `% A$ I, n8 H
5 a' M: T- {) T/ `* ]- //5 update & deactive
8 i4 |* e v6 F+ @8 {4 ^3 U5 V - sketch1->Update();
6 D [7 D) x& f' u+ G2 Z/ }( M+ Z% V
; @# Z5 ?# p6 C; l' j& S7 u# h- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);
8 `, w) M! P2 v/ t/ S/ @ - 6 K, a8 p L: Z5 M, z
- delete expression1;: w3 I( h& K9 [% e8 g
- delete expression2;
2 U; D1 s0 T& E& J2 T. ?" H- i - delete expression3;; `* U# n+ p1 A5 ^) K+ Y8 h% ?1 z( X
- ' R. Y3 O3 n3 `6 n2 C
- }
6 F1 f0 u8 r R8 k8 z6 T* O
复制代码 3 c0 r( k& p ^" R" l
6 m7 m' h% {/ X5 W- L- ]- K
: _ r" a' Z K+ I) p' r, Y
% T7 R" k1 n& ~# L6 H! _4 l |
|