Create Layer for Selected View Example (C#)
This example shows how to create a layer for the selected drawing view.
//----------------------------------------------------------------------------
// Preconditions:
// 1. Open a drawing of a part.
// 2. Add reference, Microsoft.VisualBasic, to the project.
// 3. Select a drawing view.
//
// Postconditions: Creates a layer for the selected view. To verify the
creation
// of a layer for the selected view, click the Layer Properties tool on the
// Line Format toolbar. The newly created layer is selected in the
// Layers dialog box.
//----------------------------------------------------------------------------
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
CreateLayer2_CSharp.csproj
{
partial
class
SolidWorksMacro
{
private
void
ChangeComponentLayer(DrawingDoc
swDraw, string
sLayerName)
{
bool
bRet = false;
// Form a valid layer name
sLayerName =
Strings.Replace(sLayerName,
"/",
"_", 1,
-1, 0);
sLayerName = Strings.Replace(sLayerName,
"@",
"_", 1,
-1, 0);
bRet = swDraw.CreateLayer2(sLayerName,
"Layer for part in "
+ sLayerName, 0, (int)swLineStyles_e.swLineCONTINUOUS,
(int)swLineWeights_e.swLW_NORMAL,
true,
true);
// Changes in all views
swDraw.ChangeComponentLayer(sLayerName,
true);
}
public
void Main()
{
ModelDoc2
swModel = default(ModelDoc2);
DrawingDoc
swDraw = default(DrawingDoc);
SelectionMgr
swSelMgr = default(SelectionMgr);
View
swView = default(View);
ModelDoc2
swDrawModel = default(ModelDoc2);
PartDoc
swDrawPart = default(PartDoc);
int
nErrors = 0;
int
nWarnings = 0;
swModel = (ModelDoc2)swApp.ActiveDoc;
swDraw = (DrawingDoc)swModel;
swSelMgr = (SelectionMgr)swModel.SelectionManager;
swView = (View)swSelMgr.GetSelectedObject6(1,
-1);
swDrawModel = swApp.OpenDoc6(swView.GetReferencedModelName(),
(int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"",
ref nErrors,
ref
nWarnings);
swDrawPart = (PartDoc)swDrawModel;
Debug.Print("File
= " + swModel.GetPathName());
Debug.Print(" View
= " + swView.Name);
Debug.Print(" View
Model = " + swView.GetReferencedModelName());
ChangeComponentLayer(swDraw, swView.Name);
}
public
SldWorks
swApp;
}
}