Here is the second installment of VBA examples converted to C#. This project has the following functions. (converted from VBA procedures in the help file). This blog post has the other examples in the help section on 2D Sketches.
Download InventorHelpExamples_Sketch_2
Offset
DrawSketchLine
ToggleSketchVisibility
CreateSketchBlockDefinition
InsertSketchBlockDefinition
CreateSketchBlock
CreateSketchedSymbolDefinition
InsertSketchedSymbolOnSheet
AddSymbolWithLeader
DrawSketchEllipticalArc
SketchEntities
SplineByDefinition
DrawSketchSpline
CopyBodyFromPartToPart
Here is the Offset example:
// Offset a 2D sketch API Sample
//Description
//This sample demonstrates the creation of offsets
//in 2d sketches. Two ways of creating the offset
//are shown - one uses a distance and the other
//uses the input point.
public void Offset()
{
// Check to make sure a sketch is open.
// Set a reference to the active sketch.
PlanarSketch oSketch = default(PlanarSketch);
try
{
Object actObj =
ThisApplication.ActiveEditObject;
oSketch = (PlanarSketch)actObj;
}
catch
{
MessageBox.Show("A sketch must be active.");
return;
}
// Reference the transient geometry collection.
TransientGeometry oTransGeom =
default(TransientGeometry);
oTransGeom = ThisApplication.TransientGeometry;
// Create a rectangle
SketchEntitiesEnumerator oRectangleLines =
default(SketchEntitiesEnumerator);
oRectangleLines = oSketch.SketchLines.
AddAsTwoPointRectangle(oTransGeom.
CreatePoint2d(0, 0),
oTransGeom.CreatePoint2d(10, -10));
// Create a new object collection
ObjectCollection oCollection =
default(ObjectCollection);
oCollection = ThisApplication.TransientObjects.
CreateObjectCollection();
// Add the first sketch line of the rectangle
// to the collection
oCollection.Add(oRectangleLines[1]);
// Create a point at (0,3). The entity resulting
// from the offset of the first sketch line must
// pass thru this point.
Point2d oOffsetPoint = default(Point2d);
oOffsetPoint = oTransGeom.CreatePoint2d(0, 3);
// Create an offset rectangle using the
// offset point.
oSketch.OffsetSketchEntitiesUsingPoint
(oCollection, oOffsetPoint, true);
// Get the sketch normal
UnitVector oNormalVector = default(UnitVector);
oNormalVector = oSketch.PlanarEntityGeometry.Normal;
// Get the direction of sketch line being offset
SketchEntity oSkEnt = oRectangleLines[1];
// Get the geometry associated with the sketch
// entity. Since the Geometry property will
// return one of various types of objects,
// we need to use late binding here
// which C# doesn't
// handle very nicely.
object geom = oSkEnt.GetType().
InvokeMember("Geometry",
BindingFlags.GetProperty,
null, oSkEnt, null);
UnitVector2d oLineDir =
(UnitVector2d)geom.GetType().
InvokeMember("Direction",
BindingFlags.GetProperty, null, geom, null);
UnitVector oLineVector = default(UnitVector);
oLineVector = oTransGeom.CreateUnitVector
(oLineDir.X, oLineDir.Y, 0);
// The cross product of these vectors is the
// natural offset direction for the sketch line.
UnitVector oOffsetVector = default(UnitVector);
oOffsetVector = oLineVector.
CrossProduct(oNormalVector);
// Get the desired offset vector (the +ve y-axis)
UnitVector oDesiredVector = default(UnitVector);
oDesiredVector = oTransGeom.
CreateUnitVector(0, 1, 0);
bool bNaturalOffsetDir = false;
if (oOffsetVector.IsEqualTo(oDesiredVector))
{
bNaturalOffsetDir = true;
}
else
{
bNaturalOffsetDir = false;
}
// Create an offset at a distance of 6 cms.
oSketch.OffsetSketchEntitiesUsingDistance
(oCollection, 6, bNaturalOffsetDir, true);
}
- Wayne


Subscribe