PLM之家PLMHome-工业软件与AI结合践行者

[二次开发源码] NX二次开发源码: 通过点创建样条曲线

[复制链接]

2013-10-30 14:28:35 3981 0

admin 发表于 2013-10-30 14:28:35 |阅读模式

admin 楼主

2013-10-30 14:28:35

请使用QQ关联注册PLM之家,学习更多关于内容,更多精彩原创视频供你学习!

您需要 登录 才可以下载或查看,没有账号?注册

x
NX二次开发源码: 通过点创建样条曲线
; V7 C* v  @) N/ ~( z* F
  1. #include <uf_defs.h>
    5 N$ v" Z# z( g; R4 }
  2. #include <uf.h>
    , W( s& H7 C; [4 t" B3 m
  3. #include <uf_modl.h>2 R4 B" ?: }7 |1 b( o: L: E. r3 ?% a
  4. #include <uf_object_types.h>
      |  f/ @( n8 ?6 {' H1 \. A- [
  5. #include <uf_ui.h>
    , b; u7 m- g$ A$ S' A3 r2 F; ~
  6. #include <uf_modl.h>
    9 Q3 I# V' h/ B7 X. f9 w$ s4 d
  7. #include <NXOpen/NXException.hxx>( o1 g) [$ J% }% x- U+ ]) D
  8. #include <NXOpen/Session.hxx>9 l& j( j& L8 K4 ~2 R7 g7 ~
  9. #include <NXOpen/Selection.hxx>
      B) f3 K9 B4 R1 s- h) G8 F7 U
  10. #include <NXOpen/Builder.hxx>' h, S" G6 D5 `/ q8 H
  11. #include <NXOpen/Features_Feature.hxx>
    , \1 Z, s" B4 m9 T( i' F$ c
  12. #include <NXOpen/Features_FeatureBuilder.hxx>
    5 A* D( h6 |4 \3 Y1 F2 w! n6 g
  13. #include <NXOpen/Features_FeatureCollection.hxx>
    / ]2 l* o3 f- _" u& N5 ]
  14. #include <NXOpen/Features_GeometricConstraintData.hxx>: l  N/ j% t6 F; t
  15. #include <NXOpen/Features_GeometricConstraintDataManager.hxx>' y* r9 i- \. ^/ C/ t, ~
  16. #include <NXOpen/Features_StudioSplineBuilder.hxx>
    $ K2 S- Q2 c( _, P2 U& ]
  17. #include <NXOpen/Part.hxx>$ |) [; N- `( y& u8 `/ d
  18. #include <NXOpen/ParTCollection.hxx>0 G9 @9 C/ c5 ?$ w) [+ \- k9 X
  19. #include <NXOpen/Point.hxx>: Z* U* O, ?! v& }$ F8 I/ e: {3 f! f
  20. #include <NXOpen/PointCollection.hxx>
    ) s5 f- k' c. _
  21. #include <NXOpen/NXString.hxx>
    : {+ L! E- \. x# e
  22. #include <NXOpen/UI.hxx>( ~" z# O6 x( }: x
  23. #include <NXOpen/Spline.hxx>3 F) B: G- O7 C' g
  24. #include <NXOpen/NXObjectManager.hxx>- D/ r9 V* e" ~" Z5 ]6 a* u" [

  25. + ]+ w- {9 a. B4 f% ^  q4 ~
  26. using namespace NXOpen;9 Q, ^0 x% q* \
  27. using namespace std;
    + n5 s9 k# R8 T( T  A/ N# M# G
  28. 3 ?7 S9 @7 U( D4 d
  29. extern "C" DllExport int ufusr_ask_unload()
    3 s0 o9 [3 G; R8 j  v$ m
  30. {' q) N1 p. P: ~/ I) A3 Q
  31.     return (int)Session::LibraryUnloadOptionImmediately;# O, n: M( K; t& P/ {4 l8 s
  32. }
    & X2 F5 d* W3 A* L& m9 p' K

  33. . W% u  @6 J! n9 u1 @1 Y
  34. static vector<Point *> selectPoints(NXString prompt): h* {4 e8 n* o5 l9 w+ ^5 W
  35. {
    7 N4 F2 g2 ^& J
  36.     UI *ui = UI::GetUI();
    # I9 o0 {& t& X# Y0 S+ o( v
  37.     Selection *sm = ui->SelectionManager();0 h/ x- v( v; ?9 D! \: |2 y
  38.     std::vector<Selection::MaskTriple> mask(1);$ K" ~3 y# ]3 ~- L7 F) W
  39.     mask[0] = Selection::MaskTriple(UF_point_type, 0, 0);
    3 m/ F+ j3 N0 E" x, W
  40.     std::vector<NXObject *> objects;( H; h3 b5 I( R1 O
  41. / t& M. a7 \, g2 X/ l$ g
  42.     sm->SelectObjects("Select Points", prompt,
    ( {+ i& B- w+ l. f+ ]% |2 f# x
  43.         Selection::SelectionScopeAnyInAssembly,
    1 j5 N# X" s: t6 E
  44.         Selection::SelectionActionClearAndEnableSpecific,
    + r' J( o, I7 L5 M
  45.         false, false, mask, objects);
    - S5 ?& ?( B: I, T8 p. X

  46. * S0 X, ]2 ]; h0 s" w4 V5 W
  47.     vector<Point *>selPoints(objects.size());
    * Q' A: O& V6 G, R" v/ z! d
  48.     for (unsigned int ii = 0; ii < objects.size(); ii++)$ f. p2 |( u1 D( w4 D
  49.         selPoints[ii] = dynamic_cast<Point *>(objects[ii]);
    ! p/ k& H3 N0 }8 f" q
  50. $ n: Z5 B$ f4 k, W$ e3 w
  51.     return selPoints;
    % h* ?  G! L2 }" O
  52. }
      z0 p4 ]- G5 \  X% W

  53. 5 ]3 E  E/ w5 t% Q9 y6 S# w
  54. static Spline *createStudioSplineThruPoints(vector<Point *> thePoints), V# d- X  A. D  w+ E  M
  55. {
    9 f3 U1 K7 w* b0 c7 ~" E
  56.     Session *theSession = Session::GetSession();  U% N* @0 a9 s& V. u* F. e. n& @
  57.     Part *workPart(theSession->Parts()->Work());
    1 G, @3 P1 E2 m3 ^" A
  58. ; V# K# J: t* j# m
  59.     Session::UndoMarkId markId1;9 ?: \6 |2 z) E
  60.     markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible,. n( M6 H% J: t7 H' i% B
  61.         "Studio Spline Thru Points");1 ^% A7 s! k; O( f) j  s" R2 C

  62. # X4 x6 Y6 j0 W2 H
  63.     Features::StudioSpline *nullFeatures_StudioSpline(NULL);) D4 L' [, t& X8 n

  64. 0 p% U/ D& d- @# D4 J8 |
  65.     Features::StudioSplineBuilder *studioSplineBuilder1;4 p" i7 m3 |, Q% Y# p3 \- \' m% t
  66.     studioSplineBuilder1 = workPart->Features()->8 X0 u3 {) f1 u. D0 v# X: M7 I
  67.         CreateStudioSplineBuilder(nullFeatures_StudioSpline);
    # b% _# t. \3 `4 @$ L
  68.     studioSplineBuilder1->SetAssociative(true);
    3 O* \7 Z$ n$ {% L
  69.     studioSplineBuilder1->
    9 m! W, x" r* r5 j  ]
  70.         SetInputCurveOption(Features::StudioSplineBuilder::CurveOptionRetain);
    ) _" X' W/ G$ p# g  P
  71.     studioSplineBuilder1->
    ; C, _7 g  Z; w, j; x7 ^. M
  72.         SetSplineMethod(Features::StudioSplineBuilder::MethodThroUGhPoints);6 r' [% D( G! M, D4 O
  73.     studioSplineBuilder1->SetDegree((int)thePoints.size() - 1);
    + g+ o/ {& n5 g7 f$ v6 ]
  74.     studioSplineBuilder1->SetPeriodic(false);
    / f7 S% |! Y) C, i( I+ w& p
  75.     studioSplineBuilder1->5 }! l' g/ [/ U8 }6 X# s
  76.         SetMatchKnots(Features::StudioSplineBuilder::MatchKnotsTypeNone);
    2 r6 `3 |- d3 X# @% w7 a1 s5 B
  77.     std::vector<double> knots1(0);4 n0 B) t& t$ w9 y* ?6 y0 k/ o
  78.     studioSplineBuilder1->SetKnots(knots1);4 C, D; c# J7 L  }8 j$ _
  79.     std::vector<double> parameters1(0);* m8 ?0 P8 s1 p1 f9 c
  80.     studioSplineBuilder1->SetParameters(parameters1);9 ^/ R" n1 I1 _+ u5 ?
  81.     Direction *nullDirection(NULL);1 J4 Q* B* u' X& y9 f1 j
  82.     Scalar *nullScalar(NULL);0 h3 i( s1 R5 D% g- @4 L0 \" Z
  83.     Offset *nullOffset(NULL);
    # o: N! D, i0 d3 m
  84. % y+ ?# U6 x2 ~+ _# U3 O! Z
  85.     std::vector<Features::GeometricConstraintData *>
    - |4 a# i" n5 w* P
  86.         geometricConstraintData(thePoints.size());
    & ]+ r2 q0 P# v6 Q, b
  87. , L  R! u8 d. c+ \- V( S
  88.     for (unsigned int ii = 0; ii < thePoints.size(); ii++)
    - ?0 h4 H6 h' U$ c7 J" n- z& x
  89.     {) k5 N+ i- H( y4 P6 Z) f" L0 f
  90.         geometricConstraintData[ii] = studioSplineBuilder1->
    ' \! Z- a, T  |: R: g% S1 }# _
  91.             ConstraintManager()->CreateGeometricConstraintData();
    1 X7 c/ z& V1 E5 P, c3 M
  92.         geometricConstraintData[ii]->SetPoint(thePoints[ii]);
    / u6 z7 I: |1 q$ e4 z8 y) G
  93.         geometricConstraintData[ii]->SetAutomaticConstraintDirection(
    : a* i: b8 H3 t9 Q% @) z
  94.             Features::GeometricConstraintData::ParameterDirectionIso);
    0 e9 h' ?& h0 Q" S' \, O0 D/ c
  95.         geometricConstraintData[ii]->SetAutomaticConstraintType(
    " E4 V' g. m, b' ]* X& K" r# B
  96.             Features::GeometricConstraintData::AutoConstraintTypeNone);
    " i5 V/ Y% W& c, R/ s) H, H
  97.         geometricConstraintData[ii]->SetTangentDirection(nullDirection);
    ' S. \( d5 `5 s$ E/ K$ J- u( w( h3 U
  98.         geometricConstraintData[ii]->SetTangentMagnitude(nullScalar);# R' G- N+ u9 S8 k3 @# b
  99.         geometricConstraintData[ii]->SetCurvature(nullOffset);$ q& ~% n* `3 K9 U9 R; k
  100.         geometricConstraintData[ii]->SetCurvatureDerivative(nullOffset);
    ; y$ V( ~6 ]" r: k  p, M
  101.         geometricConstraintData[ii]->SetHasSymmetricModelingConstraint(false);. o9 o' U" Z, S- n
  102.     }
    7 I* S4 I8 d! }5 z* L/ H; T

  103. . }. _6 I7 W" w  ]2 J- m
  104.     studioSplineBuilder1->ConstraintManager()->SetContents(
    4 T) M- B" U! w* |, w% Y
  105.         geometricConstraintData);: H5 ?* \  e/ b: [) ?3 L
  106. $ D) ^+ N1 |- [0 a
  107.     Features::Feature *feature1 = studioSplineBuilder1->CommitFeature();$ w1 f5 x$ ]7 C! i
  108.     Spline *theSpline = studioSplineBuilder1->Curve();" L  e' p; R5 G+ L/ I( {/ L; j
  109. - e5 G8 L2 K5 A6 a) \# N% q
  110.     studioSplineBuilder1->Destroy();5 \6 x8 w5 ?+ g9 }2 U
  111. # d3 X* g. q6 ?8 \9 |! q" M& b
  112.     return theSpline;9 S& E/ M5 o3 U9 [. c6 }
  113. }
    3 J+ @* R/ R- s  w
  114. 7 ^2 N* T4 w3 W+ r8 h& N- v6 b
  115. extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
    # m) g" T" w  J& o! i
  116. {6 U. p- y" x9 ]  \; Z
  117.     std::vector<Point *>thePoints = selectPoints("Studio Spline Thru Points");
    " w) C( S* H# a  G2 F
  118.     if (!thePoints.size()) return;& B7 i5 j) |" c$ {. h% z
  119. 9 f, o: E" A4 y, K' I3 |* T) i- Z& k
  120.     Spline *theSpline = createStudioSplineThruPoints(thePoints);
    1 J, G/ U/ x, p, r, r

  121. 9 X# x4 v3 f+ z: x4 {0 N' e
  122.     if (theSpline)
    ; Z0 o: d4 l2 {. @0 b! V
  123.     {2 q5 S3 n3 i; |" ]6 E4 K* R
  124.         theSpline->Highlight();
      E" W& C3 h& x0 j7 @* ]
  125.         uc1601("Studio Spline Created Thru Points", TRUE);3 s$ N2 e# b9 e* w' u: @0 ]5 ~
  126.         theSpline->Unhighlight();
    5 H& {, R- ~( X" x0 o7 w
  127.     }1 I* ^' s/ ~. i
  128. }
    " a4 h. M1 H" I7 x- U0 ~
复制代码

; ?1 V* K/ H6 }  x% B! u
6 U# s$ ^7 Y5 n0 ]  p9 F; s* `" Q% \; G; O6 {. I  @+ c
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 doTeam.tech
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 注册

返回列表 本版积分规则

  • 发布新帖

  • 在线客服

  • 微信

  • 客户端

  • 返回顶部

  • x
    温馨提示

    本网站(plmhome.com)为PLM之家工业软件学习官网站

    展示的视频材料全部免费,需要高清和特殊技术支持请联系 QQ: 939801026

    PLM之家NX CAM二次开发专题模块培训报名开始啦

    我知道了