|
|
请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!
您需要 登录 才可以下载或查看,没有账号?注册
x
PLM之家-NX9.0 二次开发实例视频31 NXOpen C++创建基本特征 <1>-草图的创建4 }. f# }+ P2 \0 s& v: l
内容:$ C' X2 M8 Y" p! T# q( y( V) r
草图作为参数化建模的基础,使用代码实现草图的创建过程,主要是对草图环境的初始化,以及草图约束的添加等问题。+ k: d2 M- j- V; V$ I7 A
目的:+ f5 R }4 R8 s* k7 ~
在NX中,使用NXOpen C++可以很方便的进行JA的录制,从而发掘草图约束中尺寸约束和几何约束的规律,通过这种方式,可以快速的完成开发。- ~; `' M) u6 W* N" {- u: W
NXOpen C++创建草图基本特征
! u2 h1 N* t! @$ _ 31 NXOpen C++创建基本特征-草图的创建(1)项目介绍- c# p- H5 G1 B
31 NXOpen C++创建基本特征-草图的创建(2)UI设计- _7 Q: u; Q. v4 z! z' M p
31 NXOpen C++创建基本特征-草图的创建(3)项目创建
+ n( q" s1 L$ Y: E8 H' ` 31 NXOpen C++创建基本特征-草图的创建(4)草图特征创建
! y& a. g, O2 x& `! c. V) I/ T 31 NXOpen C++创建基本特征-草图的创建(5)添加几何到草图! a7 w, l* U+ {# `/ B
31 NXOpen C++创建基本特征-草图的创建(6)添加草图几何约束* {" R3 ?/ V$ T; x$ M
31 NXOpen C++创建基本特征-草图的创建(7)添加草图尺寸约束
/ ^6 ]- T/ M1 o; O4 W. Q7 Y3 p 31 NXOpen C++创建基本特征-草图的创建(8)尺寸和UI关联6 M) V' L5 K* c# K# ~" w* ?
0 ~ m! y; w" j/ j( h* i. o
. V% ^! W6 N" a2 R; P2 T
. _ r1 N" O% B, H/ b) D- u0 j0 T9 [" @$ @
' p( I+ A! f# J# {% g7 w0 \% q关键代码分享:
I- Y, Y3 k2 c
; s' u& o8 f. n& d; o5 z7 x5 D3 p: R, B# ]
- 6 m! ~ u$ f2 [2 @1 G% x- D0 s* \
- void lesson31_createSkeTCh::createIPSketch()0 u: Z- l& v# d* b
- {
, Q( y: |& a) G$ n5 {
+ L- m' {: g% u) F- K' d- Sketch *nullSketch(NULL);
1 u: P. h" e0 ~ - SketchInPlaceBuilder *sketchInPlaceBuilder1;1 Y! m* ?5 z3 t8 J& p! Z+ {
- sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);5 g, {2 X9 o) v! j# b& G
- sketchInPlaceBuilder1->SetPlaneOption(Sketch::PlaneOptionNewPlane);6 S1 ?" S. ]0 W& j) Z
: k; A" b. J' T H6 |# {- //sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);
. c1 Q3 {2 N1 }1 P% v4 v' `( l - ; M2 s7 h# x/ O2 N
" P% r, l8 v& J3 y, ?- u5 M- // add preference. `# d8 [9 B ^, j8 Q/ Q
- . I% J, R8 Y. p4 A) `# o- w
- theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);. V% N6 Q/ Z( I! ]
3 F1 Y5 ~9 R& Z( o( s; b1 @- Drafting::PreferencesBuilder *preferencesBuilder1;
$ v; d$ k; c# n- ^- U$ u" Y, y D - preferencesBuilder1 = workPart->SettingsManager()->CreatePreferencesBuilder();, Z! m/ p5 A1 i1 @) K' [
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetAngularDimensionValuePrecision(3);$ j) @4 r2 G/ w2 J: I0 ], V) K
- preferencesBuilder1->AnnotationStyle()->DimensionStyle()->SetDimensionValuePrecision(3);
; Z$ i- E0 p( N0 i - preferencesBuilder1->Commit();0 q! a; v+ b7 m. j
x* g( r2 ^! L+ \* W+ Q* a: [- preferencesBuilder1->Destroy();
: o, N, | d7 t/ a" _
' Y7 ]1 J# g% i& S' {- 7 B- M! P( I0 R3 K7 w& J' I
- NXObject *nXObject1;
7 _, L# C; s/ N6 l' u: @1 Q8 m6 J - nXObject1 = sketchInPlaceBuilder1->Commit();& i! \. D& F/ t- i" {
- ; Y8 J7 m7 o; o; V8 V% i8 n4 \
- Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));
( e$ }; a) s1 X' D# a0 @* B - Features::Feature *feature1;
8 Z- y" K2 o8 w+ c$ ~ - feature1 = sketch1->Feature();* m, J- h& X6 k; }9 R
- feature1->SetName("XY-Plane-Sketch");* f7 q4 W5 z' {% W& x3 U, x
-
/ c: v1 W; T, d - sketchInPlaceBuilder1->Destroy();
/ W: b" v0 U* l - 6 e: E0 T' `& j8 \
- //1 active sketch , G) _' W ], e; g$ |
- , r7 C3 H* y) `7 @1 t0 {3 ~: D
- sketch1->Activate(Sketch::ViewReorientFalse);2 P* P, D/ ]+ `8 W* m! k
-
, U, b$ O2 Z w; g6 t* a - double height1 = this->expressionHeight1->GetProperties()->GetDouble("Value");
9 o! r( h) Y7 H - double height2 = this->expressionHeight2->GetProperties()->GetDouble("Value"); @; Y) \7 n0 W8 D w R" F' _1 C
- double length = this->expressionLength->GetProperties()->GetDouble("Value");: n( z8 E" t$ J. A
- // 2.1 create geometry
% Z: I- i4 D" [1 N& ]
6 ?" C9 P9 }3 C- Point3d startPoint1(0.0, 0.0, 0.0); V# G! J; @; y9 E$ @ n
- Point3d endPoint1(0, height1, 0.0);$ e4 i$ e1 D" y9 f% v$ z! Q5 k
- Point3d endPoint2(length, height1, 0.0);" L' U5 w7 I3 h
- Point3d endPoint3(length, height1-height2, 0.0);) ^& S$ x4 t3 M; U/ `$ g
- Point3d origin = sketch1->Origin();' U/ j& e2 b6 |/ m
- Point *originPoint = workPart->Points()->CreatePoint(origin);! u& P$ C4 p9 D* O8 O
- ! G' J( l5 \% G$ N
- Line *line1,*line2,*line3,*line4;
$ S" ^) X o5 L0 ^& O - line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);
@8 b& w: b3 T0 Q - line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);
8 V! E8 _+ M; Q+ L' F4 {. X - line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);1 Q+ A. v* u+ Z" b6 W
- line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
8 v8 e7 J1 z9 T3 V - // 2.2 add geometry
5 X* O+ @' I1 w3 y - 1 a2 c4 i# q( |5 `
- sketch1->AddGeometry(line1,Sketch::InferConstraintsOptionInferCoincidentConstraints);# W% j' l M4 {6 q# R( ]# j! T5 x
- sketch1->AddGeometry(line2,Sketch::InferConstraintsOptionInferCoincidentConstraints);( c3 ?: C1 b4 `9 }" W
- sketch1->AddGeometry(line3,Sketch::InferConstraintsOptionInferCoincidentConstraints);
$ W, P$ g- K& K, \( X# n1 }# n - sketch1->AddGeometry(line4,Sketch::InferConstraintsOptionInferCoincidentConstraints);
2 Y- T/ q/ ~9 k; ~+ K9 _: @: r7 } - / T, N7 ~2 b3 a) j, }; C' V* f
- //3 add constraints ( gemo)( x1 D: \# c, b/ r& C3 O
( W( W2 u, F- J# v0 g5 \- Sketch::ConstraintGeometry geomline1;, ~2 h; g$ `3 D. H' F& \
- geomline1.Geometry = line1;1 ?: D. P+ F( R0 H
- geomline1.PointType = Sketch::ConstraintPointTypeNone;
( L* E. G3 A/ f" W1 m" Q6 N5 F - geomline1.SplineDefiningPointIndex = 0;5 K* }- ~* N9 [* ]- O
- sketch1->CreateVerticalConstraint(geomline1);% I+ T9 p3 D6 k0 b. y9 R d$ [
- 0 r3 Q$ p( N, p: `$ T$ }3 u7 R( M3 {
- Sketch::ConstraintGeometry geomline2;( A3 o& q3 s7 j% q4 m( j# E0 ~5 W
- geomline2.Geometry = line2;# V0 X# F7 I2 Q8 @5 h" J* ]
- geomline2.PointType = Sketch::ConstraintPointTypeNone;
, i" F! a! g' i+ o2 D; O' x - geomline2.SplineDefiningPointIndex = 0;' B9 i5 Q$ p% n. `6 H! w% m2 c
- sketch1->CreateHorizontalConstraint(geomline2);) m* K0 ^0 c6 I7 F3 A
0 e+ h% W; Y- ^) w; |) C- Sketch::ConstraintGeometry geomline3;
9 w4 j+ T! x2 {& F - geomline3.Geometry = line3;6 c1 k' b% w j, d2 ] u/ b
- geomline3.PointType = Sketch::ConstraintPointTypeNone;9 s, _9 \6 `) o5 J6 y. ~9 L. @
- geomline3.SplineDefiningPointIndex = 0;4 d( V+ t' A) g" f& O9 v$ c" _, N1 H
- sketch1->CreateVerticalConstraint(geomline3);
1 L/ Z% [# p! V* C. q1 g" m9 q - + [4 p8 o4 Z H& u. W/ c
- Sketch::ConstraintGeometry geomline1_startPoint;
+ b' w0 \. n4 y6 v9 n) Q - geomline1_startPoint.Geometry = line1;
6 T9 D2 O; K7 z f+ i, A7 ]- P - geomline1_startPoint.PointType = Sketch::ConstraintPointTypeStartVertex;3 a; g' t) y8 N1 s4 [; X3 W9 r
- geomline1.SplineDefiningPointIndex = 0;
( z# e8 M# g: B$ {" d - Sketch::ConstraintGeometry geomline2_originPoint;
, g+ W9 {$ C- T! V: f' i Q @8 Y - geomline2_originPoint.Geometry = originPoint;
5 j# c" n( p6 I/ j4 h, w$ d y - geomline2_originPoint.PointType = Sketch::ConstraintPointTypeNone;8 p9 s' z. `) h5 t0 H1 X+ O. j) `
- geomline2_originPoint.SplineDefiningPointIndex = 0;. H7 ?' i# O" G. Q0 t( r1 d
- sketch1->CreateCoincidentConstraint(geomline1_startPoint,geomline2_originPoint);2 W; R9 s. M( [. c
. M5 A4 ?0 R7 h- //4 add constraints (dimension)
* C9 W1 D* T; Z' U - * }; \( T) k, t% n: E) K
- NXObject *nullNXObject(NULL);
& Y* ?0 J- F. t
( d" j4 i+ X$ d: o- 1 e4 I% G! p; _. Y
- stringstream s_height1,s_height2,s_length;0 F: |. Y5 q5 O: i
- s_height1 <<height1;
( _# e1 a8 O5 B: N8 i" T1 p - s_height2 <<height2;
8 @/ m. c( `% r0 { - s_length <<length;/ `3 j& W! a6 \$ V, W, a/ K0 r
9 \) n' }! h* l# M0 z3 [7 ]2 E+ r9 o- Expression *expression1,*expression2,*expression3;
9 [6 V. A' V9 F$ n: P/ p - expression1 = workPart->Expressions()->CreateSystemExpression(s_height1.str());3 k! o% ]; M# H: h: ~/ u( _" w
- expression2 = workPart->Expressions()->CreateSystemExpression(s_length.str());- \- I5 R5 p1 F( N" R/ y% X
- expression3 = workPart->Expressions()->CreateSystemExpression(s_height2.str());8 k! V' q8 x. _
- ' q2 @( ^! d. M8 C2 }
- // line1 dimension 9 e8 C: K7 b. D" x
% ~) q ^, q/ z0 {/ ~8 u- Sketch::DimensionGeometry dimLine1_startPoint;4 w1 \% H! s& y3 ~. a; E
- dimLine1_startPoint.Geometry = line1;: S: y- C- [! _
- dimLine1_startPoint.AssocType = Sketch::AssocTypeStartPoint;8 U1 W% r9 Y( C. s% S8 J' U- i
- dimLine1_startPoint.AssocValue = 0;! H/ A9 H) a- [6 U
- dimLine1_startPoint.HelpPoint.X = 0.0;
- ?+ ]8 G0 q* v+ Y. \; z5 S% Y - dimLine1_startPoint.HelpPoint.Y = 0.0;
9 h/ c I+ i9 r3 X5 T - dimLine1_startPoint.HelpPoint.Z = 0.0;
/ ^1 i; P. @& v- |0 @' E$ Q - dimLine1_startPoint.View = nullNXObject;3 t% k, k+ O& p- X s
- {9 Q$ U" R( B* ?( J* j) N- Sketch::DimensionGeometry dimLine1_endPoint;
6 P- l# n% M, ]! X6 u# A' {( Q/ C7 ? - dimLine1_endPoint.Geometry = line1;
- o$ `* r0 T( a( D% x - dimLine1_endPoint.AssocType = Sketch::AssocTypeEndPoint;
S+ t, v7 k$ d: P - dimLine1_endPoint.AssocValue = 0;
: }* k( v) E+ k" F5 {# \ - dimLine1_endPoint.HelpPoint.X = 0.0;$ Y$ g, Z5 W* R9 Q |% @# j
- dimLine1_endPoint.HelpPoint.Y = 0.0;
4 i" }6 V# \3 N0 g - dimLine1_endPoint.HelpPoint.Z = 0.0;9 [; ]3 ]6 Z6 X7 F- D- q! x# f; i
- dimLine1_endPoint.View = nullNXObject;, K- ]2 ^, M' z) Y7 |& E# F
- . m/ {# p. G; W$ W8 i% Q+ `
- Point3d dimOrigin1(-100, height1/2, 0.0);+ g: y7 }$ b8 D5 I( A# I$ L
% E8 i8 R K2 c- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine1_startPoint, dimLine1_endPoint, dimOrigin1, expression1, Sketch::DimensionOptionCreateAsDriving);
. [* k# }. ]4 M) V$ z5 b, \% a - 5 d o+ d6 B# I$ n! j/ ^6 D4 N
1 `' r7 F8 U; T) n- 9 g. ^' f5 T' R- D1 b. y
- // line2 dimension
( T$ d7 c5 s' z0 t" B' K - , j5 b+ n! Y1 |- f) {
- Sketch::DimensionGeometry dimLine2_startPoint;8 f& L; S* W0 n4 d& u
- dimLine2_startPoint.Geometry = line2;8 n5 ]6 k5 x# t' J6 ~
- dimLine2_startPoint.AssocType = Sketch::AssocTypeStartPoint;( o0 e8 c6 f+ S3 L
- dimLine2_startPoint.AssocValue = 0;! g& K* j4 O; }5 {! e4 C c3 P
- dimLine2_startPoint.HelpPoint.X = 0.0;
) Z: n. T. ~+ r7 N9 u4 ?. p- O4 r - dimLine2_startPoint.HelpPoint.Y = 0.0;
+ e# X5 Y, V0 R3 y. ^: w. l! M- S6 B - dimLine2_startPoint.HelpPoint.Z = 0.0;) E' r3 q, W2 v
- dimLine2_startPoint.View = nullNXObject;6 j9 D6 j/ I/ i, p
, s5 t9 V5 ~, @4 ~9 d" Z- Sketch::DimensionGeometry dimLine2_endPoint;( @9 q8 \7 y+ Y3 p! }. u
- dimLine2_endPoint.Geometry = line2;# g: r S1 ~+ L3 B
- dimLine2_endPoint.AssocType = Sketch::AssocTypeEndPoint;: E# `7 p$ m) T* V
- dimLine2_endPoint.AssocValue = 0;
3 r4 g3 b. y: ? - dimLine2_endPoint.HelpPoint.X = 0.0;
) K/ M6 t, w [, E% k( g - dimLine2_endPoint.HelpPoint.Y = 0.0;
- D! m! Y- Z) G" L2 W: _ - dimLine2_endPoint.HelpPoint.Z = 0.0;* o8 |. P, c$ }! @
- dimLine2_endPoint.View = nullNXObject;6 q0 o- B- ^: e$ }
- ' W, \- k9 |$ ?4 U% ~) ^
- Point3d dimOrigin2(length /2,100 + height1, 0.0);
* @3 I. T6 T! t* k- o$ }
1 P0 E% A% S/ g N8 n* l: R' g- sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine2_startPoint, dimLine2_endPoint, dimOrigin2, expression2, Sketch::DimensionOptionCreateAsDriving);
; y' Q: E& q9 s$ A2 j# V$ a
3 B- H+ T* ]. H3 d3 [6 K- ) m. S2 B5 H2 M0 z; R
-
) q2 x% N! Q. g s$ A' d+ A - // line3 dimension , v7 z6 A0 S+ ]
# T4 W" N a8 I' C, N, l- }- Sketch::DimensionGeometry dimLine3_startPoint;
6 m' N) U: r7 N2 l9 h0 h - dimLine3_startPoint.Geometry = line3;
) u ^3 P' D) j; Q& G5 `8 O7 V& ^ - dimLine3_startPoint.AssocType = Sketch::AssocTypeStartPoint;9 P) l* p5 W& K" O1 u
- dimLine3_startPoint.AssocValue = 0;
. I4 w5 o7 I6 y/ d9 ] - dimLine3_startPoint.HelpPoint.X = 0.0;, V& M8 O. F0 R; _+ s
- dimLine3_startPoint.HelpPoint.Y = 0.0;, P& Z& ?% n2 u0 h# @) o+ D8 i
- dimLine3_startPoint.HelpPoint.Z = 0.0;- S# r9 E. Q5 y/ w
- dimLine3_startPoint.View = nullNXObject;
' ]; d" J$ M/ q - ! l2 i y" A: e3 `
- Sketch::DimensionGeometry dimLine3_endPoint;
' ]' H/ ~2 ~: o9 E; U, y3 e - dimLine3_endPoint.Geometry = line3;* H* H6 x; d0 y5 O; X
- dimLine3_endPoint.AssocType = Sketch::AssocTypeEndPoint;
2 p5 z; w5 C" s3 G7 Y! `# ^ - dimLine3_endPoint.AssocValue = 0;
) ~1 [; J+ s, w _4 i4 Y - dimLine3_endPoint.HelpPoint.X = 0.0;5 Z! F2 y7 J! e: G5 n7 B3 j; l
- dimLine3_endPoint.HelpPoint.Y = 0.0;
' o" ~& n) v! [& s; ]0 T - dimLine3_endPoint.HelpPoint.Z = 0.0;5 `" z" g& ?( o. H6 u( l7 u/ l
- dimLine3_endPoint.View = nullNXObject;: m2 a+ D: _. d
9 j! Y& N( Z' {; C7 E- Point3d dimOrigin3( 100 + length, height1-height2+ height2 /2 , 0.0);
6 z0 ~, W7 _( M5 `& ]7 M& Q5 q -
2 d2 j- {( P# U! X3 ~) v - sketch1->CreateDimension(Sketch::ConstraintTypeParallelDim, dimLine3_startPoint, dimLine3_endPoint, dimOrigin3, expression3, Sketch::DimensionOptionCreateAsDriving);
# P* Z9 C, n J" G4 l6 ~ - ' o; b( y2 z1 M4 Y# _! g1 L
- 4 N6 B. X3 i; m
W M# I, e& F. W% J- //5 update & deactive# R. D: }3 K0 q
- sketch1->Update();+ }4 I+ k/ s) N9 D6 h8 ?0 f- h. f
- 2 t) @6 {( u$ U: z( B2 A [
- sketch1->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);
& f' _9 O- C2 c
U) G. d( R* z B, l3 S- delete expression1;
5 [3 e' O u2 [ - delete expression2;' N$ {; G+ \/ F/ N, Z3 D% n- r( K5 @! V
- delete expression3;) R" d+ O, W8 [) p% i' \
- 3 W( d9 Z4 n. k- U" i9 L
- }/ x& ?5 j S& x- D/ r3 t
复制代码
: Y5 K% }- i$ B: Y, S) Y" G: k& k" Y' s/ r
9 m: g8 N# d) l3 t |
$ W8 X0 l, }0 {: t- T+ S0 K |
|