Hide Table of Contents

Change Color Using Color Picker Example (C#)

This example shows how to change the Color of a Line using the color picker.

//--------------------------------------------------------------
// Preconditions:
// 1. Create a C# Windows console project.
// 2. Copy and paste this code into the C# project.
// 3. Add a reference to
//    install_dir\APISDK\tlb\DraftSight.Interop.dsAutomation.dll.
// 4. Add a reference to System.Windows.Forms.
// 5. Start DraftSight.
// 6. Press F5.
//
// Postconditions: 
// 1. Gets the active document.
// 2. Constructs a Line.
// 3. Gets the Color of the Line.
// 4. Opens the color picker.
// 5. Select a Color in the color picker and click OK.
// 6. Sets the Color of the Line to the Color that you selected 
//    in the color picker.
//----------------------------------------------------------------
 
using System;
using DraftSight.Interop.dsAutomation;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace ColorPickerCSharp
{
    class Program
    {
        static void Main()
        {
            DraftSight.Interop.dsAutomation.Application dsApp;
            Document dsDoc = default(Document);
            ColorPicker dsColorPicker = default(ColorPicker);
            SketchManager dsSketchMgr = default(SketchManager);
            Line dsLine = default(Line);
            Color dsColor = default(Color);
            bool OnlyColorBoxIndex = true;
            bool EnableByBlockAndLayer = true;
 
            //Connects to DraftSight
            dsApp = (DraftSight.Interop.dsAutomation.Application)Marshal.GetActiveObject("DraftSight.Application");
 
            //Aborts any command currently running in DraftSight to avoid nested commands
            dsApp.AbortRunningCommand();
 
            //Gets active document
            dsDoc = dsApp.GetActiveDocument();
 
            //Gets the Sketch Manager
            dsSketchMgr = dsDoc.GetModel().GetSketchManager();
 
            //Constructs a Line
            double StartX = 0, StartY = 0, StartZ = 0;
            double EndX = 23.6, EndY = 12.545, EndZ = 0;
            dsLine = dsSketchMgr.InsertLine(StartX, StartY, StartZ, EndX, EndY, EndZ);
 
            //Gets Color of Line
            dsColor = dsLine.Color;
 
            //Creates an instance of the color picker
            dsColorPicker = dsApp.CreateColorPicker(OnlyColorBoxIndex, EnableByBlockAndLayer); 
 
            //Sets Color to the color picker
            dsColorPicker.SetColor(dsColor);
 
            //Opens the color picker 
            dsColorPicker.Execute();
 
            //Select a Color in the color picker 
            //Click OK
 
            //Gets Color from color picker
            dsColor = dsColorPicker.GetColor();
 
            //Sets Color of the Line to Color selected in color picker
            dsLine.Color = dsColor;
        }
    }
}


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:   Change Color Using Color Picker 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) 2019 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.