Move Annotations to First Annotation View Example (C#)
This example shows how to move
annotations to the Unassigned Items annotation view.
//----------------------------------------------
// Preconditions:
// 1. Open a part or assembly document.
// 2. Add a note to the *Top annotation view.
// 3. Add a note to the *Front annotation view.
// NOTES:
// * If necessary, create *Top and *Front
// annotation views.
// * If prompted to turn on feature dimensions
// display, click No.
// 4. Open the Immediate window.
// 5. Run the macro.
//
// Postconditions:
// 1. Prints the names of the annotation views and notes
// to the Immediate window.
// 2. Moves both notes to the Unassigned Items
// annotation view.
// 3. Double-click each annotation view in the Annotations
// folder in the FeatureManager design tree to verify.
//----------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace IAnnotationViewCSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 swModel = default(ModelDoc2);
ModelDocExtension swModelExt = default(ModelDocExtension);
object[] swAnnViews = null;
object[] annotations = null;
Annotation[] annToMove = new Annotation[2];
AnnotationView swAnnView = default(AnnotationView);
Annotation swAnn = default(Annotation);
Feature swFeat = default(Feature);
int i = 0;
int j = 0;
int k = 0;
bool status = false;
swModel = (ModelDoc2)swApp.ActiveDoc;
swModelExt = (ModelDocExtension)swModel.Extension;
swAnnViews = (object[])swModelExt.AnnotationViews;
//Activate each view
//Only one view can be active at a time
for (i = 0; i <= swModelExt.AnnotationViewCount - 1; i++)
{
swAnnView = (AnnotationView)swAnnViews[i];
swFeat = (Feature)swAnnView;
Debug.Print("Annotation view name: " + swFeat.Name);
swAnnView.Activate();
//Alternative to activate
//swAnnView.ActivateAndReorient();
}
//Print each annotation name and add all annotations to an array
for (i = 0; i <= swModelExt.AnnotationViewCount - 1; i++)
{
swAnnView = (AnnotationView)swAnnViews[i];
swAnnView.Activate();
annotations = (object[])swAnnView.Annotations;
if ((annotations == null) == false)
{
for (j = 0; j <= annotations.Length - 1; j++)
{
swAnn = (Annotation)annotations[j];
annToMove[j] = (Annotation)swAnn;
Debug.Print("Annotation name: " + swAnn.GetName());
}
}
//Move all annotations to first annotation view, Unassigned Items
if (i > 0)
{
swAnnView = (AnnotationView)swAnnViews[i];
status = swAnnView.MoveAnnotations(annToMove);
Debug.Assert(status);
}
}
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}