C# Help Examples for Features – Part 2

Here is the second group of VBA examples in this section of samples for features converted to C#. The project with the first set of converted samples for features is here.

image

This project has the following functions.

 Download InventorHelpExamples_Features_2

HoleFeatureLinearPlacement
StitchFeatureCreate
CreateNonPlanarSectionLoft
EditExtrudeFeature
SweepFeature
TrueSweepLength
ThreadSample
EditThread
test_threadinfo

Here is HoleFeatureLinearPlacement function:

//    Hole feature linear placement API Sample 
//Description 
//This sample demonstrates the creation of a 
//hole feature using the linear placement type.
public void HoleFeatureLinearPlacement()
{
    // Create a new part document, using the 
    //default part template.
    PartDocument oPartDoc = default(PartDocument);
    oPartDoc = (PartDocument)ThisApplication.
        Documents.Add
            (DocumentTypeEnum.kPartDocumentObject,
      ThisApplication.FileManager.GetTemplateFile
        (DocumentTypeEnum.kPartDocumentObject));
 
    // Set a reference to the component definition.
    PartComponentDefinition oCompDef =
                  default(PartComponentDefinition);
    oCompDef = oPartDoc.ComponentDefinition;
 
    // Create a new sketch on the X-Y work plane.
    PlanarSketch oSketch = default(PlanarSketch);
    oSketch = oCompDef.Sketches.Add
                        (oCompDef.WorkPlanes[3]);
 
    // Set a reference to the 
    //transient geometry object.
    TransientGeometry oTransGeom =
                        default(TransientGeometry);
    oTransGeom = ThisApplication.TransientGeometry;
 
    // Create a square on the sketch.
    oSketch.SketchLines.AddAsTwoPointRectangle
        (oTransGeom.CreatePoint2d(0, 0),
        oTransGeom.CreatePoint2d(6, 6));
 
    // Create the profile.
    Profile oProfile = default(Profile);
    oProfile = oSketch.Profiles.AddForSolid();
 
    // Create an extrusion.
    ExtrudeDefinition oExtrudeDef =
                    default(ExtrudeDefinition);
    oExtrudeDef = oCompDef.Features.
        ExtrudeFeatures.CreateExtrudeDefinition
                                     (oProfile,
       PartFeatureOperationEnum.kJoinOperation);
    oExtrudeDef.SetDistanceExtent("2 cm",
               PartFeatureExtentDirectionEnum.
                      kNegativeExtentDirection);
    ExtrudeFeature oExtrude =
                         default(ExtrudeFeature);
    oExtrude = oCompDef.Features.ExtrudeFeatures.
                                Add(oExtrudeDef);
 
    // Get the start face of the extrude.
    Face oFace = default(Face);
    oFace = oExtrude.StartFaces[1];
 
    // Get two adjacent edges on the start face.
    Edge oEdge1 = default(Edge);
    Edge oEdge2 = default(Edge);
    oEdge1 = oFace.Edges[1];
    oEdge2 = oFace.Edges[2];
 
    // Create a bias point for hole placement to 
    //place it at the expected location. This is 
    //the model point corresponding to the center
    //of the square in the sketch.
    Inventor.Point oBiasPoint =
                        default(Inventor.Point);
    oBiasPoint = oSketch.SketchToModelSpace
            (oTransGeom.CreatePoint2d(1.5, 1.5));
 
    // Create the hole feature placement definition.
    LinearHolePlacementDefinition oLinearPlacementDef
            = default(LinearHolePlacementDefinition);
    oLinearPlacementDef = oCompDef.Features.
    HoleFeatures.CreateLinearPlacementDefinition
        (oFace, oEdge1, "2 cm",
                         oEdge2, "2 cm", oBiasPoint);
 
    // Create the hole feature.
    oCompDef.Features.HoleFeatures.
        AddDrilledByThroughAllExtent
        (oLinearPlacementDef, "1 cm",
            PartFeatureExtentDirectionEnum.
                     kPositiveExtentDirection);
}

Comments

One response to “C# Help Examples for Features – Part 2”

  1. Wayne,can you attach the parameters generated by default in the inventor application (d1,d2…) in C# code?
    Thanks,David

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading