Hide Table of Contents

Create Block Definition and Insert Block Instance Example (C#)

This example shows how to create a block definition and insert a block instance in a drawing.

 

using System;

using System.Collections.Generic;

using System.Text;

using SldWorks;

using SwConst;

//---------------------------------------------------------------------------

//

// Preconditions:

//       (1) SolidWorks is running and drawing is open.

//       (2) This project has references to sldworks.tlb and swconst.tlb.

//

// Postconditions:

//       (1) Block definition is created.

//       (2) First instance of the block is inserted in the upper-left

//           corner of the drawing sheet.

//       (3) Second instance of the block is inserted into the

//           drawing sheet.

//

// NOTE: Customize the creation of the entities to suit your situation.

//

//---------------------------------------------------------------------------

namespace SketchBlocks

{

    class Program

    {

        static void Main(string[] args)

        {

            SldWorks.SldWorks swApp = new SldWorks.SldWorks();

            ModelDoc2 swModel;

            ModelDoc2 swModel2;

            DrawingDoc swDraw;

            SketchSegment[] swSkSeg = new SketchSegment[14];

            SketchPoint[] swSkPt = new SketchPoint[4];

            Note[] swSkNote = new Note[3];

            Object vSkSeg;

            Object vSkPt;

            Object vSkNote;

            Boolean bRet;

            

            SketchBlockDefinition swSketchBlockDef;

            SketchBlockDefinition swSketchBlockDef2;

            SketchBlockInstance swBlockInst;

            SketchManager swSketchMgr;

            ModelDocExtension swModelDocExt;

            MathUtility swMathUtil;

            MathPoint swMathPoint;

            MathPoint swMathPoint2;

            MathPoint swMathPoint3;

            double[] nPt = new double[3];

            Object vPt;

            long nbrSelObjects;

            Object vInstPt;

 

            swApp = (SldWorks.SldWorks)System.Runtime.InteropServices.Marshal.GetActiveObject("SldWorks.Application");

            swModel = swApp.IActiveDoc2;

            swDraw = (DrawingDoc)swModel;

            //Make a copy of the open drawing

            //Use the path and name of your drawing

            string CopyName = "C:\Samples\Copy.SLDDRW";

            swModel.SaveAsSilent(CopyName,true);

            

            //Interfaces needed for block APIs

            swSketchMgr = swModel.SketchManager;

            swModelDocExt = swModel.Extension;

            swMathUtil = swApp.IGetMathUtility();

            //--------------------------------------------------------------------

            //Turn off grid and entity snapping

            swModel.SetAddToDB(true);

            // Revision box

            swSkSeg[0] = (SketchSegment)swModel.CreateLine2(0.008372353756316, 0.207929860362, 0, 0.05495488122133, 0.207929860362, 0);

            swSkSeg[1] = (SketchSegment)swModel.CreateLine2(0.05495488122133, 0.207929860362, 0, 0.05495488122133, 0.1992788195471, 0);

            swSkSeg[2] = (SketchSegment)swModel.CreateLine2(0.05495488122133, 0.1992788195471, 0, 0.008372353756316, 0.1992788195471, 0);

            swSkSeg[3] = (SketchSegment)swModel.CreateLine2(0.008372353756316, 0.1992788195471, 0, 0.008372353756316, 0.207929860362, 0);

            swSkSeg[4] = (SketchSegment)swModel.CreateLine2(0.023613610833382, 0.207929860362, 0, 0.023613610833382, 0.1992788195471, 0);

            swSkSeg[5] = (SketchSegment)swModel.CreateLine2(0.03964919362569, 0.207929860362, 0, 0.03964919362569, 0.1992788195471, 0);

            //--------------------------------------------------------------------

            // Clear selections; otherwise, notes are attached to line

            swModel.ClearSelection2(true);

            // Revision notes

            swSkNote[0] = (Note)swModel.InsertNote("Cell 1");

            swSkNote[1] = (Note)swModel.InsertNote("Cell 2");

            swSkNote[2] = (Note)swModel.InsertNote("Cell 3");

            PositionNote(swModel, swSkNote[0], 0.009481461553102, 0.2052680016497, 0.0);

            PositionNote(swModel, swSkNote[1], 0.025613610833382, 0.2052680016497, 0.0);

            PositionNote(swModel, swSkNote[2], 0.04275469545669, 0.2052680016497, 0.0);

        

            // Points for circles

            swSkPt[0] = (SketchPoint)swModel.CreatePoint2(0.02700536474232, 0.1708856599494, 0);

            swSkPt[1] = (SketchPoint)swModel.CreatePoint2(0.02700536474232, 0.1815330947985, 0);

            swSkPt[2] = (SketchPoint)swModel.CreatePoint2(0.03964919362569, 0.1815330947985, 0);

            swSkPt[3] = (SketchPoint)swModel.CreatePoint2(0.05029662847483, 0.1708856599494, 0);

         

            // Circles

            swSkSeg[6] = (SketchSegment)swModel.CreateCircle2(swSkPt[0].X, swSkPt[0].Y, swSkPt[0].Z, 0.03050393605009, 0.169349494074, 0.0);

            swSkSeg[7] = (SketchSegment)swModel.CreateCircle2(swSkPt[1].X, swSkPt[1].Y, swSkPt[1].Z, 0.03305243799009, 0.183621104938, 0.0);

            swSkSeg[8] = (SketchSegment)swModel.CreateCircle2(swSkPt[2].X, swSkPt[2].Y, swSkPt[2].Z, 0.04426584652606, 0.182092003774, 0.0);

            swSkSeg[9] = (SketchSegment)swModel.CreateCircle2(swSkPt[3].X, swSkPt[3].Y, swSkPt[3].Z, 0.05496955467404, 0.164252490194, 0.0);

            // Lines between circles

            swSkSeg[10] = (SketchSegment)swModel.CreateLine2(swSkPt[0].X, swSkPt[0].Y, swSkPt[0].Z, swSkPt[1].X, swSkPt[1].Y, swSkPt[1].Z);

            swSkSeg[11] = (SketchSegment)swModel.CreateLine2(swSkPt[1].X, swSkPt[1].Y, swSkPt[1].Z, swSkPt[2].X, swSkPt[2].Y, swSkPt[2].Z);

            swSkSeg[12] = (SketchSegment)swModel.CreateLine2(swSkPt[2].X, swSkPt[2].Y, swSkPt[2].Z, swSkPt[3].X, swSkPt[3].Y, swSkPt[3].Z);

            swSkSeg[13] = (SketchSegment)swModel.CreateLine2(swSkPt[3].X, swSkPt[3].Y, swSkPt[3].Z, swSkPt[0].X, swSkPt[0].Y, swSkPt[0].Z);

            vSkSeg = swSkSeg;

            vSkPt = swSkPt;

            vSkNote = swSkNote;

            // Select all sketch segments, sketch points,

            // and notes for the block definition

            nbrSelObjects = swModelDocExt.MultiSelect(vSkSeg, true, null);

            nbrSelObjects = swModelDocExt.MultiSelect(vSkPt, true, null);

            nbrSelObjects = swModelDocExt.MultiSelect(vSkNote, true, null);

            //--------------------------------------------------------------------

            //Create block definition

            swSketchBlockDef = swSketchMgr.MakeSketchBlockFromSelected(null);

            //Save block

            //Use the path and name of your drawing

            swSketchBlockDef.Save("C:\\Samples\TestBlock.SLDBLK");

            //Open copied drawing

            swApp.OpenDoc(CopyName,(int)swDocumentTypes_e.swDocDRAWING);

            swModel2 = (ModelDoc2)swApp.ActiveDoc;

            swSketchMgr = swModel2.SketchManager;

            // Define an insertion point

            nPt[0] = 0.0 / 1000.0;

            nPt[1] = 0.0 / 1000.0;

            nPt[2] = 0.0;

            vPt = nPt;

            swMathPoint2 = (MathPoint)swMathUtil.CreatePoint(vPt);

            //Create block definition from block file

            //Use the path and name of your drawing

            swSketchBlockDef2 = (SketchBlockDefinition)swSketchMgr.MakeSketchBlockFromFile(swMathPoint2, "C:\\Samples\\TestBlock.SLDBLK", false, 100, 0);

            // Define an insertion point

            nPt[0] = 60.0 / 1000.0;

            nPt[1] = 60.0 / 1000.0;

            nPt[2] = 0.0;

            vPt = nPt;

            swMathPoint = (MathPoint)swMathUtil.CreatePoint(vPt);

            // Insert an instance of the block definition

            swBlockInst = swSketchMgr.InsertSketchBlockInstance(swSketchBlockDef2, swMathPoint, 0, 0);

            //--------------------------------------------------------------------

            // Turn on grid and entity snapping

            //swModel.SetAddToDB(false);

            // Redraw to see all changes

            swModel2.GraphicsRedraw2();

        }

        static private void PositionNote(ModelDoc2 swModel, Note swSkNote, double X_pos, double Y_pos, double Z_pos)

        {

            Annotation swAnn;

            long nRetVal;

            bool bRet;

    

            swAnn = (Annotation)swSkNote.GetAnnotation();

    

            swSkNote.Angle = 0.0;

            bRet = swSkNote.SetBalloon((int)swBalloonStyle_e.swBS_None, (int)swBalloonFit_e.swBF_Tightest);

            nRetVal = swAnn.SetLeader2(false, (int)swLeaderSide_e.swLS_SMART, true, false, false, false);

            bRet = swAnn.SetPosition(X_pos, Y_pos, Z_pos);

    

            // Redraw to see changes; however, this is expensive

            swModel.GraphicsRedraw2();

        }

    }

}



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:   Create Block Definition and Insert Block Instance 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.