Hide Table of Contents

Get Centers of Mass in Drawing Views Example (C#)

This example shows how to get the centers of mass in drawing views.

//----------------------------------------------------------------------
// Preconditions: Open a drawing that has one or more centers of mass.
//
// Postconditions: Inspect the Immediate window for the coordinates of
// all of the centers of mass in all of the views of the
// drawing.
//----------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace CenterOfMass_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
ModelDoc2 swModel;
        
DrawingDoc swDrawDoc;
        
View swView;
        
CenterOfMass swCenterOfMass;
        
Annotation swAnnotation;
        
int sheetCount;
        
int viewCount;
        
object[] ss;
        
object[] vv;
        
double[] coord;

        
public void Main()
        {
            swModel = (
ModelDoc2)swApp.ActiveDoc;
            swDrawDoc = (
DrawingDoc)swModel;

            viewCount = swDrawDoc.GetViewCount();
            ss = (
object[])swDrawDoc.GetViews();

            
for (sheetCount = ss.GetLowerBound(0); sheetCount <= ss.GetUpperBound(0); sheetCount++)
            {
                vv = (
object[])ss[sheetCount];
                
for (viewCount = 1; viewCount <= vv.GetUpperBound(0); viewCount++)
                {
                    
Debug.Print(((View)vv[viewCount]).GetName2());
                    swView = (
View)vv[viewCount];
                    swCenterOfMass = (
CenterOfMass)swView.GetFirstCenterOfMass();
                    
if ((swCenterOfMass != null))
                    {
                        swAnnotation = (
Annotation)swCenterOfMass.GetAnnotation();
                        
if ((swAnnotation != null))
                        {
                            
Debug.Print("  Annotation name: " + swAnnotation.GetName());
                        }
                        coord = (
double[])swCenterOfMass.GetCoordinates();
                        
Debug.Print("  Center of mass coordinates: X: " + coord[0] + ", Y: " + coord[1] + ", and Z: " + coord[2]);

                        
while ((swCenterOfMass.GetNext() != null))
                        {
                            swCenterOfMass = (
CenterOfMass)swCenterOfMass.GetNext();
                            
if ((swCenterOfMass != null))
                            {
                                swAnnotation = (
Annotation)swCenterOfMass.GetAnnotation();
                                
if ((swAnnotation != null))
                                {
                                    
Debug.Print("  Annotation name: " + swAnnotation.GetName());
                                }
                                coord = (
double[])swCenterOfMass.GetCoordinates();
                                
Debug.Print("  Center of mass coordinates: X: " + coord[0] + ", Y: " + coord[1] + ", and Z: " + coord[2]);
                            }
                        }
                    }
                }

            }

        }

        
public SldWorks swApp;

    }
}

 



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Get Centers of Mass in Drawing Views Example (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2013 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.