查看: 4177|回复: 0

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

[复制链接]

6

主题

4

回帖

60

积分

管理员

积分
60
发表于 2013-10-30 14:28:35 | 显示全部楼层 |阅读模式

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

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

×
NX二次开发源码: 通过点创建样条曲线  s! O. j5 d% ~- U5 Y5 b) O
  1. #include <uf_defs.h>0 D) d6 U5 x+ l1 L+ A7 U( E" K4 q
  2. #include <uf.h>
    ( J. y; j& @5 ?7 C: J6 G+ |% ?$ |
  3. #include <uf_modl.h>
      @# t$ U" B  K- Y* h0 p
  4. #include <uf_object_types.h>! `, E* {. r- M* H- O
  5. #include <uf_ui.h>
    : v+ X4 `; w) h& @# l
  6. #include <uf_modl.h>$ y, A$ n+ o9 i) y
  7. #include <NXOpen/NXException.hxx>
    4 E8 w+ v7 A4 P
  8. #include <NXOpen/Session.hxx>
    2 X* V3 M& I: Y3 k+ t3 F: L' G, p
  9. #include <NXOpen/Selection.hxx>) V4 `; `: {0 J1 h( S
  10. #include <NXOpen/Builder.hxx>5 o7 c8 F, K0 k* }# F* ^9 o" O+ Y
  11. #include <NXOpen/Features_Feature.hxx>
    5 t& i; n4 L0 {( w/ o, s  h
  12. #include <NXOpen/Features_FeatureBuilder.hxx>4 X) _2 s1 z0 i! o7 K: ]
  13. #include <NXOpen/Features_FeatureCollection.hxx># M3 z0 |8 c& d& ^- {
  14. #include <NXOpen/Features_GeometricConstraintData.hxx>8 s1 L1 a$ l9 g) h& q% v
  15. #include <NXOpen/Features_GeometricConstraintDataManager.hxx>
    2 \+ @5 d* \' ?, X7 o
  16. #include <NXOpen/Features_StudioSplineBuilder.hxx>3 X8 E5 _3 k% p7 m1 p
  17. #include <NXOpen/Part.hxx>  z) I. X3 H) p( u2 O2 Q/ ~8 h
  18. #include <NXOpen/ParTCollection.hxx>- Q, z: d* K5 l! I
  19. #include <NXOpen/Point.hxx>( R/ E1 R' m& X7 u. k
  20. #include <NXOpen/PointCollection.hxx>1 N: u* Q5 d- a2 s/ G7 z1 P
  21. #include <NXOpen/NXString.hxx>( c9 z/ _$ E% w; F0 A. Y
  22. #include <NXOpen/UI.hxx>
    % [& ]5 n9 \. ]! v, ~
  23. #include <NXOpen/Spline.hxx>
    4 s2 N+ w5 Y* D& Z4 t! u  ]1 H. ^
  24. #include <NXOpen/NXObjectManager.hxx>
    " ~( u( X( h" k! F8 h+ v
  25. 4 z& b( ~- s8 O, g& w
  26. using namespace NXOpen;- D. R  n8 @" j0 a8 e
  27. using namespace std;; _% U. s% E" q3 h

  28. . a7 ]2 W) B/ R0 ]" n. F
  29. extern "C" DllExport int ufusr_ask_unload()$ U3 E8 I( Z% V3 y
  30. {
    5 }7 h6 i; Z, m& f( ~
  31.     return (int)Session::LibraryUnloadOptionImmediately;1 o2 C! E  Z8 h' L7 F
  32. }
    7 M8 o! w- b8 E# U8 E

  33. 6 n# J& k: A- Q2 e$ d$ @) J9 q. D
  34. static vector<Point *> selectPoints(NXString prompt)# v: _$ B9 W2 d. G7 {0 S, c0 P
  35. {( j8 y8 f% B3 S
  36.     UI *ui = UI::GetUI();
    # c0 g& z" T" L. t7 L2 g
  37.     Selection *sm = ui->SelectionManager();
    7 f) v9 f& r4 F3 k6 W1 |
  38.     std::vector<Selection::MaskTriple> mask(1);1 Q. e6 p0 R6 x5 ]
  39.     mask[0] = Selection::MaskTriple(UF_point_type, 0, 0);2 D" _# @, m1 J; s8 K3 |( X6 M5 L( w
  40.     std::vector<NXObject *> objects;
    ) W5 U, f( ~+ `9 w
  41. 0 C4 J9 O" W" ?6 {4 \
  42.     sm->SelectObjects("Select Points", prompt,  E2 t- ?8 s: }9 R% _* s
  43.         Selection::SelectionScopeAnyInAssembly,
    * |. o' M/ k7 v
  44.         Selection::SelectionActionClearAndEnableSpecific,
    0 s8 Z3 f5 |5 w4 R$ k$ c9 ]5 c& P
  45.         false, false, mask, objects);
    % Y6 W$ {7 P# R. N

  46. 9 ]( r# U; V" M" Z# G
  47.     vector<Point *>selPoints(objects.size());- V/ {1 ?9 r5 g1 E! j
  48.     for (unsigned int ii = 0; ii < objects.size(); ii++)
    $ s# m4 ~: h4 ]; a* D0 K8 e% I
  49.         selPoints[ii] = dynamic_cast<Point *>(objects[ii]);( k9 C& v1 T& Z$ I

  50. / z; f: Z2 k: S: O1 l
  51.     return selPoints;8 [% ]8 j1 r+ I/ m7 c# d
  52. }, ?. x' b7 Q- P3 M0 y: A8 V

  53. . O' G2 n1 R4 [: B- N( ]
  54. static Spline *createStudioSplineThruPoints(vector<Point *> thePoints)
    9 \, B. o* e+ w( u2 t2 y
  55. {: _% I, N  m/ r# h& @/ ?; B
  56.     Session *theSession = Session::GetSession();) z, i6 A# y$ C  O8 H  O6 S
  57.     Part *workPart(theSession->Parts()->Work());$ R# H$ x. N; g+ |

  58. + A- ~2 i1 I- e2 B! U/ q
  59.     Session::UndoMarkId markId1;
    ; Y- b8 [' D* j( p1 F
  60.     markId1 = theSession->SetUndoMark(Session::MarkVisibilityVisible,
    9 V- i( i' I! Y; O$ T, @* ]# M7 _9 Y
  61.         "Studio Spline Thru Points");/ M, a% ^) M5 a0 j9 ?9 Y
  62. ; q! v* W# @  g4 N
  63.     Features::StudioSpline *nullFeatures_StudioSpline(NULL);: }% o. ]: s2 N1 f1 L7 `
  64. 2 E. A5 u5 K* Y3 E
  65.     Features::StudioSplineBuilder *studioSplineBuilder1;
    ) G/ l; ?% ]3 J( j8 @. o
  66.     studioSplineBuilder1 = workPart->Features()->. h- I7 c+ j, w. F0 A
  67.         CreateStudioSplineBuilder(nullFeatures_StudioSpline);5 x- j4 K+ L2 m8 |* f$ Y! e# y
  68.     studioSplineBuilder1->SetAssociative(true);
    - W, y* t6 o2 X) W' H# T3 O2 i4 y
  69.     studioSplineBuilder1->/ x; h6 E5 t/ t! b+ d3 J
  70.         SetInputCurveOption(Features::StudioSplineBuilder::CurveOptionRetain);, O5 Y3 b) g6 U; K, q& n$ R' v% ?2 G
  71.     studioSplineBuilder1->
    2 z! f3 [2 ^/ g. q8 g6 \  O
  72.         SetSplineMethod(Features::StudioSplineBuilder::MethodThroUGhPoints);
    ! s" X0 {; `0 z+ o3 \! M
  73.     studioSplineBuilder1->SetDegree((int)thePoints.size() - 1);$ L, W5 y* q2 R
  74.     studioSplineBuilder1->SetPeriodic(false);  P0 v+ ~9 G$ w1 D7 `! A' y. ?
  75.     studioSplineBuilder1->
    ! P7 E' {7 X; N4 p$ @
  76.         SetMatchKnots(Features::StudioSplineBuilder::MatchKnotsTypeNone);8 `0 e* W: v- Y
  77.     std::vector<double> knots1(0);
    1 g( R, p% e( b' ^
  78.     studioSplineBuilder1->SetKnots(knots1);1 B5 A$ |1 x0 p* A( R  g
  79.     std::vector<double> parameters1(0);5 h* N* s' s4 ?2 s* s
  80.     studioSplineBuilder1->SetParameters(parameters1);  C, ^! R  `7 `  o1 M4 b' M/ u6 r
  81.     Direction *nullDirection(NULL);( N& R. k+ `: e( Q  o% {
  82.     Scalar *nullScalar(NULL);' {0 o: A- O2 m3 M" V
  83.     Offset *nullOffset(NULL);
    , u9 U$ q, T+ y' z
  84. 5 K* g% Z" M: [
  85.     std::vector<Features::GeometricConstraintData *>
    / c: y: \: r0 g+ X( O; l3 a/ i+ @# Q/ I
  86.         geometricConstraintData(thePoints.size());4 f" P; x8 g) F3 s0 ]  ~
  87. 0 Z% T/ S& W" G$ u) o
  88.     for (unsigned int ii = 0; ii < thePoints.size(); ii++)
    2 Z  Q/ F4 X4 |: A
  89.     {3 {( N* ]! {. T' ^
  90.         geometricConstraintData[ii] = studioSplineBuilder1->* C* A5 j" T- o
  91.             ConstraintManager()->CreateGeometricConstraintData();
    " S8 B7 x; `, i% s
  92.         geometricConstraintData[ii]->SetPoint(thePoints[ii]);
    0 }% y# T% w- M" h
  93.         geometricConstraintData[ii]->SetAutomaticConstraintDirection(2 ?9 g. _& `8 ]# G. x& ?
  94.             Features::GeometricConstraintData::ParameterDirectionIso);7 m' e' H5 S' Y' h
  95.         geometricConstraintData[ii]->SetAutomaticConstraintType(" H4 R( ^( d: w! F9 }; g7 P* p( ?
  96.             Features::GeometricConstraintData::AutoConstraintTypeNone);
      }1 N) v& ^9 {: s/ S- {$ _0 M
  97.         geometricConstraintData[ii]->SetTangentDirection(nullDirection);
    * @& z& _) c5 @: w3 ~  `4 p
  98.         geometricConstraintData[ii]->SetTangentMagnitude(nullScalar);- e7 g& v, Z9 ^- t& i0 ?
  99.         geometricConstraintData[ii]->SetCurvature(nullOffset);2 p( @% {1 O& D' d1 W
  100.         geometricConstraintData[ii]->SetCurvatureDerivative(nullOffset);
    / Y5 z9 r5 ~  A, U$ N" j  @! W7 |
  101.         geometricConstraintData[ii]->SetHasSymmetricModelingConstraint(false);( R4 q6 ^' h: h* H
  102.     }4 ~  l8 t6 f* A- c& n

  103. ' r: ?9 m' M& Z7 K* _9 [8 a
  104.     studioSplineBuilder1->ConstraintManager()->SetContents(
    ; N  {- p- D5 l# R# f
  105.         geometricConstraintData);* H1 W2 P" s/ F$ n* ?- ]

  106. " i: K% f$ \: d7 ?. z8 W
  107.     Features::Feature *feature1 = studioSplineBuilder1->CommitFeature();
    0 Q0 i  l( \) i! m# L2 |6 r
  108.     Spline *theSpline = studioSplineBuilder1->Curve();
    : v6 t) g$ r3 u" d+ {
  109. 9 }6 `$ _4 o1 M& g* D
  110.     studioSplineBuilder1->Destroy();
    + N) x0 g, m3 r0 u/ ?7 z* S
  111. 8 w  M5 g, z) w! _  {, J0 f6 Y( E8 F; p
  112.     return theSpline;
    3 |5 n( K, a5 Q+ D& o* b
  113. }
    4 c+ H* |7 r0 V. j3 V2 ~9 t; }
  114. 6 [, ?/ K( n9 {# `# o7 Z' \
  115. extern "C" DllExport void ufusr(char *param, int *retcod, int param_len)
    3 ]0 Y0 U) f* _
  116. {
    7 `/ W6 v1 ~7 \1 U& X. I3 E
  117.     std::vector<Point *>thePoints = selectPoints("Studio Spline Thru Points");+ ]6 S- q8 }$ r# D8 |
  118.     if (!thePoints.size()) return;
    4 l5 n1 i. y: Q: R0 @

  119. / H) d6 M0 a- o- c& \, s2 z! p
  120.     Spline *theSpline = createStudioSplineThruPoints(thePoints);
    * I: ]  P# g: S' O

  121. % z3 B7 y) S* V5 p8 N0 [1 c6 r
  122.     if (theSpline)
    : W2 Z8 f  m% x/ p1 G# i% G. S9 {
  123.     {* s7 h6 a( [3 F3 u/ [
  124.         theSpline->Highlight();+ v4 g  O& |4 X6 _' j
  125.         uc1601("Studio Spline Created Thru Points", TRUE);
      q2 F6 q+ {( t, b5 G* N6 I
  126.         theSpline->Unhighlight();
    . m% \; `5 [) F8 r3 D# j
  127.     }# ?7 T3 T# _% F  k
  128. }
    ! ]0 u' M( r/ N6 n  K
复制代码
  ?( o; q- m3 \, g
0 Q! V: N& w2 x) T

, z/ v( c" Q, y2 V- C) o; \! G
上海点团信息科技有限公司,承接UG NX,CATIA,CREO,Solidworks 等CAx软件,Teamcenter,3D Experience等PLM软件,工业4.0数字化软件的实施\二次开发\培训相关业务,详情QQ 939801026 Tel 18301858168 网址 www.doteam.tech
回复

使用道具 举报

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

本版积分规则

在本版发帖
关注公众号
QQ客服返回顶部