Hide Table of Contents

Rotate and Copy Entities Example (C#)

This example shows how to rotate and copy selected entities.

//--------------------------------------------------------------
// Preconditions:
// 1. Create a C# Windows console project.
// 2. Copy and paste this example into the C# IDE.
// 3. Add a reference to:
//    install_dir\APISDK\tlb\DraftSight.Interop.dsAutomation.dll.
// 4. Add references to System and System.Windows.Forms.
// 5. Start DraftSight and open a new document.
// 6. Construct multiple entities (e.g., circle, rectangle, 2D PolyLine,
//    etc.).
// 7. Start debugging the project.
//
// Postconditions:
// 1. When the prompt appears in the DraftSight command window,
//    select the entities to rotate and press the Enter key. The
//    selected entities are rotated.
// 2. Execution stops so that you can examine the drawing to
//    verify that the selected entities were rotated. Click the
//    Continue button in the IDE to continue.
// 3. The selected entities are copied.
//----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using DraftSight.Interop.dsAutomation;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;

namespace RotateSelectedEntities
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//Connect to DraftSight application
            DraftSight.Interop.dsAutomation.Application dsApp = ConnectToDraftSight();
            
if (null == dsApp)
            {
                
return;
            }

           
dsApp.AbortRunningCommand(); // abort any command currently running in DraftSight to avoid nested commands

            
//Get active document
            Document dsDoc = dsApp.GetActiveDocument();
            
if (null == dsDoc)
            {
                
MessageBox.Show("There are no open documents in DraftSight.");
                
return;
            }

            
//Get model space
            Model dsModel = dsDoc.GetModel();

            
//Get Sketch Manager
            SketchManager dsSketchMgr = dsModel.GetSketchManager();

            
//Get selection manager
            SelectionManager dsSelectionMgr = dsDoc.GetSelectionManager();

            
//Get selection filter
            SelectionFilter dsSelectionFilter = dsSelectionMgr.GetSelectionFilter();

            
//Clear selection filter
            dsSelectionFilter.Clear();

            
//Add all entities to the selection filter
            foreach (dsObjectType_e entityType in Enum.GetValues(typeof(dsObjectType_e)))
            {
                dsSelectionFilter.AddEntityType(entityType);
            }

            
//Activate selection filter
            dsSelectionFilter.Active = true;

            
//Get command message object
            CommandMessage dsCommandMessage = dsApp.GetCommandMessage();

            
//Clear previous selection
            dsSelectionMgr.ClearSelections(dsSelectionSetType_e.dsSelectionSetType_Previous);

            
//Run prompt to select entities
            bool singleSelection = false;
            
string prompt = "Select entities";
            
string errorMessage = "Unknown entity";
            
if (dsCommandMessage.PromptForSelection(singleSelection, prompt, errorMessage))
            {
                
//Get number of selected objects
                int count = dsSelectionMgr.GetSelectedObjectCount(dsSelectionSetType_e.dsSelectionSetType_Previous);

                
DispatchWrapper[] dsEntities = new DispatchWrapper[count];
                
int[] dsEntityTypes = new int[count];

                
//Get selected entities
                for (int index = 0; index < count; ++index)
                {
                    
dsObjectType_e entityType;
                    
object selectedEntity = dsSelectionMgr.GetSelectedObject(dsSelectionSetType_e.dsSelectionSetType_Previous, index, out entityType);
                    dsEntities[index] =
new DispatchWrapper(selectedEntity);

                    dsEntityTypes[index] = (
int)entityType;
                }

                
//Rotation parameters
                double pivotPointX = 0.0;
                
double pivotPointY = 0.0;
                
double rotateAngle = Math.PI / 4; //In radians

                //Rotate entities
                dsSketchMgr.RotateEntities(pivotPointX, pivotPointY, rotateAngle, dsEntityTypes, dsEntities);

                
//Stop execution
                //Examine the document
                System.Diagnostics.Debugger.Break();

                
//Click the Continue button in the IDE

                //Copy parameters
                double displacementX = 2.0;
                
double displacementY = 2.0;
                
double displacementZ = 0.0;

                
//Copy entities
                object[] dsEntityCopies = dsSketchMgr.CopyEntities2(displacementX, displacementY, displacementZ, dsEntityTypes, dsEntities);


            }
        }

        
private static DraftSight.Interop.dsAutomation.Application ConnectToDraftSight()
        {
            

            DraftSight.Interop.dsAutomation.Application dsApp = null;

            
try
            {
                
//Connect to DraftSight
                dsApp = (DraftSight.Interop.dsAutomation.Application)Marshal.GetActiveObject("DraftSight.Application");
            }
            
catch (Exception ex)
            {
                
MessageBox.Show("Failed to connect to DraftSight. Cause: " + ex.Message);
                dsApp =
null;
            }

            
return dsApp;
        }
    }
}



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:   Rotate and Copy Entities 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) 2023 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.