This code will generate an empty folder before a selected component or feature only (not contained inside a folder). Not able to find any API codes for placing a folder before a folder.
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim swFeat As SldWorks.Feature
Dim bRet As Boolean
Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swFeatMgr = swModel.FeatureManager
Set swFeat = swFeatMgr.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
' Return NULL if unable to create the folder
If Not swFeat Is Nothing Then
swFeat.Name = swFeat.Name & "_NEW"
As Deepak says there is no direct API for doing this. Furthermore it is explicitly mentioned that the Folder feature cannot be used as the reference for other folder feature.
I have written some trick-macro which actually does the case. Please find it in the attachment. Hope it helps.
Application Engineer at Intercad
Tel: +61 2 9454 4444
InsertFolderToTheTop.swp.zip 8.9 KB
Hi Artem. Cool solution!
I went a different way:
while traverse on components, run CreateFolders ()
private string SelectFirstComp()
Feature swFeature1 = (Feature)swModel.FirstFeature();
for (int i = 0; i < 100; i++)
string typeFeat = swFeature1.GetTypeName();
if (typeFeat == "Reference")
string assyName = swModel.GetTitle().Substring(0, swModel.GetTitle().Length - 7);
string featName = swFeature1.Name;
string fullName = featName + "@" + assyName;
swModel.Extension.SelectByID2(fullName, "COMPONENT", 0, 0, 0, true, 0, null, 0);
swFeature1 = (Feature)swFeature1.GetNextFeature();
private void CreateFolders()
foreach (string name in Names)
string myComp = SelectFirstComp();
Feature feature = default(Feature);
FeatureManager featureMgr = (FeatureManager)swModel.FeatureManager;
feature = featureMgr.InsertFeatureTreeFolder2((int)swFeatureTreeFolderType_e.swFeatureTreeFolder_EmptyBefore);
feature.Name = name;
folderUge = true;
In this case folders must be empty.
I mean, first create all the necessary folders, and then fill them.
Not able to find any API codes for placing a folder before a folder.
So I went a little different way. Create a folder, and then drag and drop them. But the solution Artem can help me deal with the problem.