About your second question of getting the occurrence you want to rotate. You don't have to use a user selection but can get it from traversing the model hierarchy. The code below will get an occurrence named "RotateTest" at the top level of the assembly. There are other techniques that you can use to label something and find it later using "attributes" but it sounds like just using the name of the occurrence as you see it in the browser will probably work in your case.
Inventor.ComponentOccurrence occ = asmDoc.ComponentDefinition.Occurrences.ItemByName["RotateTest"];
Below is a C# version of the previous program. I converted it into a function so you just pass in the occurence and the angle you want to rotate it.
public bool rotateOcc(Inventor.ComponentOccurrence occ, double angle)
{
try
{
Inventor.Application invApp = occ.Application;
// Get a reference to the TransientGeometry object.
Inventor.TransientGeometry tg = invApp.TransientGeometry;
// Check that the part only has a single insert constraint.
if( (occ.Constraints.Count == 1) && (occ.Constraints[1] is Inventor.InsertConstraint) && (occ.Joints.Count == 0) && !occ.Grounded)
{
Inventor.InsertConstraint insert = (Inventor.InsertConstraint) occ.Constraints[1];
// Get the geometry from the geometry the constraint is tied to.
Inventor.Point origin = null;
Inventor.UnitVector zAxis = null;
if (insert.GeometryOne is Inventor.Circle)
{
Inventor.Circle circle = (Inventor.Circle)insert.GeometryOne;
origin = circle.Center;
zAxis = circle.Normal;
}
else if (insert.GeometryOne is Inventor.Arc3d)
{
Inventor.Arc3d arc = (Inventor.Arc3d)insert.GeometryOne;
origin = arc.Center;
zAxis = arc.Normal;
}
//** Define a transformation matrix based on the constraint geometry.
// Create a temporary X axis in an arbitrary direction.
Inventor.UnitVector xAxis = tg.CreateUnitVector(zAxis.X + 1, zAxis.Y + 1, zAxis.Z + 1);
// Cross the two existing vectors to create the Y axis.
Inventor.UnitVector yAxis = zAxis.CrossProduct(xAxis);
// Now create the correct X axis by crossing the Y and Z axes.
xAxis = yAxis.CrossProduct(zAxis);
// Create a matrix using the coordinate system defined above. This defines
// a coordinate system positioned at the current location of the selected
// part where the Z axis lies along the rotation axis.
Inventor.Matrix originalTrans = tg.CreateMatrix();
originalTrans.SetCoordinateSystem(origin, xAxis.AsVector(), yAxis.AsVector(), zAxis.AsVector());
// Invert the matrix to get a matrix that defines a transform to go from the current position
// of the part to the origin where the defined coordinate system lines up with the model coordinate system.
Inventor.Matrix inverseTrans = originalTrans.Copy();
inverseTrans.Invert();
// Create the matrix that goes from the current position to the origin.
Inventor.Matrix newTrans = occ.Transformation;
newTrans.TransformBy(inverseTrans);
// Define a matrix that defines the rotation around the Z axis.
Inventor.Matrix rotateTrans = tg.CreateMatrix();
rotateTrans.SetToRotation(angle, tg.CreateVector(0.0, 0.0, 1.0), tg.CreatePoint(0.0, 0.0, 0.0));
// Apply the rotation to the full tranform.
newTrans.TransformBy(rotateTrans);
// Apply the original transform to the full transform to move the part back into it's original position.
newTrans.TransformBy(originalTrans);
// Apply the matrix to the part.
occ.Transformation = newTrans;
return true;
}
else
{
return false;
} }
catch (Exception)
{
return false;
}
}