admin 发表于 2014-2-19 17:06:32

NX二次开发源码: 创建草图并通过草图做出回转体

通过NX开发进行草图的创建,创建草图过程包括基本曲线的创建以及约束。
草图的约束分为几何约束和尺寸约束,通过代码的约束使得草图完全约束。
草图完成后,可以通过回转体进行旋转,此处直接做出简单的例子。
仅供参考!

效果如下:


admin 发表于 2014-2-19 17:07:47

比较乱,仅供参考


<p>void basicFeatures::createSketch()
{</p><p>{
    Session *theSession = Session::GetSession();
    Part *workPart(theSession->Parts()->Work());
    Part *displayPart(theSession->Parts()->Display());
    Sketch *nullSketch(NULL);
    SketchInPlaceBuilder *sketchInPlaceBuilder1;
    sketchInPlaceBuilder1 = workPart->Sketches()->CreateNewSketchInPlaceBuilder(nullSketch);
    sketchInPlaceBuilder1->Plane()->SetMethod(PlaneTypes::MethodTypeFixedZ);
    Point3d origin1(0.0, 0.0, 0.0);
    sketchInPlaceBuilder1->Plane()->SetOrigin(origin1);
    sketchInPlaceBuilder1->Plane()->Evaluate();</p><p> // set the reference</p><p> theSession->Preferences()->Sketch()->SetCreateInferredConstraints(true);
   
    theSession->Preferences()->Sketch()->SetContinuousAutoDimensioning(false);
   
    theSession->Preferences()->Sketch()->SetDimensionLabel(Preferences::SketchPreferences::DimensionLabelTypeExpression);
   
    theSession->Preferences()->Sketch()->SetTextSizeFixed(true);
   
    theSession->Preferences()->Sketch()->SetFixedTextSize(3.0);
   
    theSession->Preferences()->Sketch()->SetConstraintSymbolSize(3.0);
   
    theSession->Preferences()->Sketch()->SetDisplayObjectColor(false);
   
    theSession->Preferences()->Sketch()->SetDisplayObjectName(true);</p><p>   
    NXObject *nXObject1;
    nXObject1 = sketchInPlaceBuilder1->Commit();
   
    Sketch *sketch1(dynamic_cast<Sketch *>(nXObject1));
    Features::Feature *feature1;
    feature1 = sketch1->Feature();</p><p>
    sketchInPlaceBuilder1->Destroy();</p><p>
sketch1->Activate(Sketch::ViewReorientFalse);

// define the dimensions
    double heigth1 = this->doubleHeight1->GetProperties()->GetDouble("Value");
double heigth2 = this->doubleHeight2->GetProperties()->GetDouble("Value");
double length = this->doubleLength->GetProperties()->GetDouble("Value");
char msg1,msg2,msg3;
sprintf(msg1,"%f",heigth1);
sprintf(msg2,"%f",heigth2);
sprintf(msg3,"%f",length);
Expression *height1exp,*height2exp,*lengthexp;
height1exp= workPart->Expressions()->CreateSystemExpression(msg1);
height2exp= workPart->Expressions()->CreateSystemExpression(msg2);
lengthexp= workPart->Expressions()->CreateSystemExpression(msg3);
// these for the dimension position
Point3d dimOrigin1(-100, heigth1/2, 0.0);
Point3d dimOrigin2(length/2, heigth1+100, 0.0);
Point3d dimOrigin3(length+100, heigth1-heigth2/2, 0.0);</p><p>// add curves
    Point3d startPoint1(0.0, 0.0, 0.0);
Point3d endPoint1(0.0,heigth1,0.0);
Point3d endPoint2(length,heigth1,0.0);
Point3d endPoint3(length,heigth1-heigth2,0.0);
    Line *line1,*line2,*line3,*line4;
    line1 = workPart->Curves()->CreateLine(startPoint1, endPoint1);
line2 = workPart->Curves()->CreateLine(endPoint1, endPoint2);
line3 = workPart->Curves()->CreateLine(endPoint2, endPoint3);
line4 = workPart->Curves()->CreateLine(endPoint3, startPoint1);
theSession->ActiveSketch()->AddGeometry(line1, Sketch::InferConstraintsOptionInferCoincidentConstraints);
theSession->ActiveSketch()->AddGeometry(line2, Sketch::InferConstraintsOptionInferCoincidentConstraints);
theSession->ActiveSketch()->AddGeometry(line3, Sketch::InferConstraintsOptionInferCoincidentConstraints);
theSession->ActiveSketch()->AddGeometry(line4, Sketch::InferConstraintsOptionInferCoincidentConstraints);
   </p><p> // add constraints
//..
// for line1
Sketch::ConstraintGeometry geopoint1;
geopoint1.Geometry = line1;
geopoint1.PointType = Sketch::ConstraintPointTypeStartVertex;
geopoint1.SplineDefiningPointIndex = 0;
// find the (0,0,0) point
Sketch::ConstraintGeometry geopoint2;
Point *pointOriginal;
pointOriginal = workPart->Points()->CreatePoint(sketch1->Origin());
geopoint2.Geometry = pointOriginal;
geopoint2.PointType = Sketch::ConstraintPointTypeStartVertex;
geopoint2.SplineDefiningPointIndex = 0;
theSession->ActiveSketch()->CreateCoincidentConstraint(geopoint1,geopoint2);</p><p> Sketch::ConstraintGeometry geoline1;
geoline1.Geometry = line1;
geoline1.PointType = Sketch::ConstraintPointTypeNone;
geoline1.SplineDefiningPointIndex = 0;
theSession->ActiveSketch()->CreateVerticalConstraint(geoline1);
//..
// for line2
Sketch::ConstraintGeometry geoline2;
geoline2.Geometry = line2;
geoline2.PointType = Sketch::ConstraintPointTypeNone;
geoline2.SplineDefiningPointIndex = 0;
theSession->ActiveSketch()->CreateHorizontalConstraint(geoline2);
//..
// for line3
Sketch::ConstraintGeometry geoline3;
geoline3.Geometry = line3;
geoline3.PointType = Sketch::ConstraintPointTypeNone;
geoline3.SplineDefiningPointIndex = 0;
theSession->ActiveSketch()->CreateVerticalConstraint(geoline3);
// use this method to create the constraints
/*SketchConstraintBuilder *line3constraint;
line3constraint= workPart->Sketches()->CreateConstraintBuilder();
line3constraint->GeometryToConstrain()->Add(line3);
line3constraint->SetConstraintType(SketchConstraintBuilder::ConstraintVertical);
line3constraint->Commit();
    line3constraint->Destroy();*/
   
// add dimension
//..
// for line1
Sketch::DimensionGeometry dimobject1_start;
dimobject1_start.AssocType = Sketch::AssocTypeStartPoint;
dimobject1_start.AssocValue = 0;
dimobject1_start.Geometry = line1;
dimobject1_start.HelpPoint.X = 0 ;
dimobject1_start.HelpPoint.Y = 0 ;
dimobject1_start.HelpPoint.Z = 0 ;
NXObject *nullNXObject1(NULL);
dimobject1_start.View = nullNXObject1;</p><p> Sketch::DimensionGeometry dimobject1_end;
dimobject1_end.AssocType = Sketch::AssocTypeEndPoint;
dimobject1_end.AssocValue = 0;
dimobject1_end.Geometry = line1;
dimobject1_end.HelpPoint.X = 0 ;
dimobject1_end.HelpPoint.Y = 0 ;
dimobject1_end.HelpPoint.Z = 0 ;
dimobject1_end.View = nullNXObject1;</p><p> SketchDimensionalConstraint *sketchDimensionalConstraint1 = theSession->ActiveSketch()->CreateDimension(Sketch::ConstraintTypeVerticalDim,dimobject1_start,dimobject1_end,dimOrigin1,height1exp,Sketch::DimensionOptionCreateAsDriving);</p><p>    Annotations::Dimension *dimension1;
    dimension1 = sketchDimensionalConstraint1->AssociatedDimension();</p><p>
//..
// for line2
Sketch::DimensionGeometry dimobject2_start;
dimobject2_start.AssocType = Sketch::AssocTypeStartPoint;
dimobject2_start.AssocValue = 0;
dimobject2_start.Geometry = line2;
dimobject2_start.HelpPoint.X = 0 ;
dimobject2_start.HelpPoint.Y = 0 ;
dimobject2_start.HelpPoint.Z = 0 ;
dimobject2_start.View = nullNXObject1;</p><p> Sketch::DimensionGeometry dimobject2_end;
dimobject2_end.AssocType = Sketch::AssocTypeEndPoint;
dimobject2_end.AssocValue = 0;
dimobject2_end.Geometry = line2;
dimobject2_end.HelpPoint.X = 0 ;
dimobject2_end.HelpPoint.Y = 0 ;
dimobject2_end.HelpPoint.Z = 0 ;
dimobject2_end.View = nullNXObject1;</p><p> SketchDimensionalConstraint *sketchDimensionalConstraint2 = theSession->ActiveSketch()->CreateDimension(Sketch::ConstraintTypeHorizontalDim,dimobject2_start,dimobject2_end,dimOrigin2,lengthexp,Sketch::DimensionOptionCreateAsDriving);</p><p>    Annotations::Dimension *dimension2;
    dimension2 = sketchDimensionalConstraint2->AssociatedDimension();</p><p> // for line3
Sketch::DimensionGeometry dimobject3_start;
dimobject3_start.AssocType = Sketch::AssocTypeStartPoint;
dimobject3_start.AssocValue = 0;
dimobject3_start.Geometry = line3;
dimobject3_start.HelpPoint.X = 0 ;
dimobject3_start.HelpPoint.Y = 0 ;
dimobject3_start.HelpPoint.Z = 0 ;
dimobject3_start.View = nullNXObject1;</p><p> Sketch::DimensionGeometry dimobject3_end;
dimobject3_end.AssocType = Sketch::AssocTypeEndPoint;
dimobject3_end.AssocValue = 0;
dimobject3_end.Geometry = line3;
dimobject3_end.HelpPoint.X = 0 ;
dimobject3_end.HelpPoint.Y = 0 ;
dimobject3_end.HelpPoint.Z = 0 ;
dimobject3_end.View = nullNXObject1;</p><p> SketchDimensionalConstraint *sketchDimensionalConstraint3 = theSession->ActiveSketch()->CreateDimension(Sketch::ConstraintTypeVerticalDim,dimobject3_start,dimobject3_end,dimOrigin3,height2exp,Sketch::DimensionOptionCreateAsDriving);</p><p>    Annotations::Dimension *dimension3;
    dimension3 = sketchDimensionalConstraint3->AssociatedDimension();</p><p> //workPart->Expressions()->Delete(height1exp);</p><p> theSession->ActiveSketch()->Update();</p><p> theSession->ActiveSketch()->Deactivate(Sketch::ViewReorientFalse,Sketch::UpdateLevelModel);

/*</p><p> // revolve the body</p><p> */
Features::Feature *nullFeatures_Feature(NULL);
Features::RevolveBuilder *revolveBuilder1;
    revolveBuilder1 = workPart->Features()->CreateRevolveBuilder(nullFeatures_Feature);
    revolveBuilder1->Limits()->StartExtend()->Value()->SetRightHandSide("0");
    revolveBuilder1->Limits()->EndExtend()->Value()->SetRightHandSide("360");
    revolveBuilder1->SetTolerance(0.01);
Section *section1;
    section1 = workPart->Sections()->CreateSection(0.0095, 0.01, 0.5);
    revolveBuilder1->SetSection(section1);
   
section1->SetAllowedEntityTypes(Section::AllowTypesOnlyCurves);
   
std::vector<Features::Feature *> features1(1);
    features1 = feature1;
    CurveFeatureRule *curveFeatureRule1;
    curveFeatureRule1 = workPart->ScRuleFactory()->CreateRuleCurveFeature(features1);
   
    section1->AllowSelfIntersection(false);
   
    std::vector<SelectionIntentRule *> rules1(1);
    rules1 = curveFeatureRule1;
    NXObject *nullNXObject(NULL);
    Point3d helpPoint1(0.0, 0.0, 0.0);
    section1->AddToSection(rules1, nullNXObject, nullNXObject, nullNXObject, helpPoint1, Section::ModeCreate, false);

// define the axis</p><p>    Direction *direction1;
    direction1 = workPart->Directions()->CreateDirection(line2, SenseForward, SmartObject::UpdateOptionWithinModeling);
   
    Point *nullPoint(NULL);
    Axis *axis1;
    axis1 = workPart->Axes()->CreateAxis(nullPoint, direction1, SmartObject::UpdateOptionWithinModeling);

revolveBuilder1->SetAxis(axis1);</p><p> // commit feature
Features::Feature *feature2;
    feature2 = revolveBuilder1->CommitFeature();
revolveBuilder1->Destroy();
}</p><p>}
</p><p> </p>
页: [1]
查看完整版本: NX二次开发源码: 创建草图并通过草图做出回转体