Get Where Annotations Can Be Shown Example (C#)
This example shows how to get the names of the annotation views where an
annotation can be shown.
//----------------------------------------------
// Preconditions:
// 1. Open a part document with one or more annotations
// in one or more annotation views.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Gets the annotation views.
// 2. Iterates through the annotation views and annotations.
// 3. Prints the name of each annotation view and number of
// annotations in that annotation view.
// 4. Prints whether each annotation in that annotation view
// can be shown in the other annotation views and in
// multiple annotation views.
// 5. Examine the Immediate window.
//----------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
namespace CanShowCSharp.csproj
{
public partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 swModel = default(ModelDoc2);
ModelDocExtension swModelExt = default(ModelDocExtension);
object[] swAnnViews = null;
object[] annotations = null;
AnnotationView swAnnView = default(AnnotationView);
Annotation swAnn = default(Annotation);
Feature swFeat = default(Feature);
int i = 0;
int j = 0;
int k = 0;
int nbrAnnotations = 0;
swModel = (ModelDoc2)swApp.ActiveDoc;
swModelExt = (ModelDocExtension)swModel.Extension;
//Get the annotation views
swAnnViews = (object[])swModelExt.AnnotationViews;
for (i = 0; i <= swModelExt.AnnotationViewCount - 1; i++)
{
swAnnView = (AnnotationView)swAnnViews[i];
}
Debug.Print("Number of annotation views = " + swModelExt.AnnotationViewCount);
Debug.Print("");
//Iterate through the annotation views and annotations
//Print the name of each annotation view and number of annotations
//in that annotation view
//Print whether each annotation in that annotation view
//can be shown in the other annotation views and in
//multiple annotation views
Debug.Print(" Name of annotation view and number of annotations in the annotation view ");
Debug.Print("");
for (i = 0; i <= swModelExt.AnnotationViewCount - 1; i++)
{
swAnnView = (AnnotationView)swAnnViews[i];
swAnnView.Activate();
annotations = (object[])swAnnView.GetAnnotations2(false, false);
swFeat = (Feature)swAnnView;
nbrAnnotations = swAnnView.AnnotationCount;
if (nbrAnnotations > 0)
{
Debug.Print("");
}
Debug.Print(" " + swFeat.Name + " = " + nbrAnnotations);
if ((annotations != null))
{
j = 0;
for (j = 0; j <= nbrAnnotations - 1; j++)
{
Debug.Print(" Can show annotation " + (j + 1) + " in these annotation views: ");
swAnn = (Annotation)annotations[j];
for (k = 0; k <= swModelExt.AnnotationViewCount - 1; k++)
{
swAnnView = (AnnotationView)swAnnViews[k];
swFeat = (Feature)swAnnView;
Debug.Print(" " + swFeat.Name + " = " + swAnn.CanShowInAnnotationView(swFeat.Name));
}
Debug.Print(" multiple = " + swAnn.CanShowInMultipleAnnotationViews());
Debug.Print("");
}
}
}
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}