Select Entity in Drawing View Example (C#)
This example shows how to select any of these entities in a drawing
view: model face, edge, or vertex.
//----------------------------------------------------------------------------
// Problem:
// Selection of a model geometry in the context of a drawing
// view can be quite problematic. To address this, use
// IView::SelectEntity
//
// Thus, given an entity in the context of the model, this
// method selects the entity in the context of the drawing
// view.
//
// This code shows how to use this method to assist in
// adding a dimension to a drawing view.
//
// Preconditions:
// 1. Part or assembly is open.
// 2. Assembly is fully resolved.
// 3. Specified template exists.
// 4. Face, edge or vertex is selected.
//
// Postconditions:
// 1. New drawing is created with three views.
// 2. If possible, the previously selected face, edge or vertex
// is dimensioned in the first drawing view.
//
// NOTE: The dimension is not guaranteed to be created if, for
// example, a face is selected.
//----------------------------------------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using
System.Runtime.InteropServices;
using
System;
namespace
AddDimensionToEntity_CSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
const
string
sPathToTemplate = "C:\\Program Files\\SolidWorks\\data\\templates\\drawing.drwdot";
const
double
nYoffset = 0.01;
ModelDoc2
swModel = default(ModelDoc2);
SelectionMgr
swSelMgr = default(SelectionMgr);
Entity
swEnt = default(Entity);
DrawingDoc
swDraw = default(DrawingDoc);
ModelDoc2
swDrawModel = default(ModelDoc2);
View
swView = default(View);
double[]
vOutline = null;
DisplayDimension
swDispDim = default(DisplayDimension);
double
nXpos = 0;
double
nYpos = 0;
bool
bRet = false;
swModel = (ModelDoc2)swApp.ActiveDoc;
swSelMgr = (SelectionMgr)swModel.SelectionManager;
swEnt = (Entity)swSelMgr.GetSelectedObject6(1,
-1);
swDraw = (DrawingDoc)swApp.NewDrawing2((int)swDwgTemplates_e.swDwgTemplateCustom,
sPathToTemplate, (int)swDwgPaperSizes_e.swDwgPaperA1size,
0.0, 0.0);
swDrawModel = (ModelDoc2)swDraw;
bRet = swDraw.Create3rdAngleViews2(swModel.GetPathName());
swView = (View)swDraw.GetFirstView();
swView = (View)swView.GetNextView();
bRet = swView.SelectEntity(swEnt,
false);
// Work out where to place
dimension -
//
midway across view and slightly above
vOutline = (double[])swView.GetOutline();
nXpos = (vOutline[0] + vOutline[2]) / 2.0;
nYpos = vOutline[3] + nYoffset;
// This depends on the
orientation of the entity in the drawing view.
//
Thus, could be NULL.
//
//
Will also create the dimension even if the entity is not
//
visible in the drawing view
swDispDim = (DisplayDimension)swDrawModel.AddDimension2(nXpos,
nYpos, 0.0);
}
public
SldWorks
swApp;
}
}