如何将.iges文件导入c# [英] How to import .iges Files to c#

查看:168
本文介绍了如何将.iges文件导入c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的

我想将iges文件(CAD文件)导入C#。

但是我不知道该怎么办。

我发现这个代码但是我很困惑。

这个例子说明如何:

指定级别和值用于导入IGES数据。
导入IGES文件。
// -------------------- ----------------------------
// 前提条件:替换路径和名称
//
//
// 后置条件:
// 1.创建名为Layer 25的文件夹。
// 2.将指定的IGES文件导入SolidWorks
// 并将导入的IGES功能移动到第25层。
// 3. To验证,检查图表ics区域和
// FeatureManager设计树。
< span class =code-comment> // ------------------------- -------
使用 SolidWorks.Interop.sldworks;
使用 SolidWorks.Interop.swconst;
使用 System.Runtime.InteropServices;
使用系统;
使用 System.Diagnostics;

namespace ImportIGESFilesCSharp.csproj
{
public 部分 SolidWorksMacro
{
public void Main()
{

ModelDoc2 model = default (ModelDoc2);
bool boolstatus = false ;
string fileName = null ;
string argString = null ;
ImportIgesData importData = 默认(ImportIgesData);
int Err = 0 ;
bool orgSetting = false ;
bool allLevels = false ;
object vOnlyLev = null ;
int [] onlyLev = new int [ 2 ];
int oneLev = 0 ;
功能lastFeature = 默认(功能);
功能newFolder = 默认(功能);
string newFolderName = ;
string lastFeatureName = null ;


model =(ModelDoc2)swApp.ActiveDoc;
// 替换IGES文件的路径和名称
fileName = < span class =code-string>
C:\\beam_with_holes.igs;
// r表示打开新文档
// i表示插入现有文档
如果(model == null
{
argString = R;
// 现有部分,请使用
}
else
{
argString = i 的;
}
// 填写导入数据
importData = (ImportIgesData)swApp.GetImportFileData(文件名);
if ((importData!= null ))
{
// 测试各种标志
importData.IncludeSurfaces = ;
importData.IncludeCurves = true ;
importData.CurvesAsSketches = true ;
// False =曲线为曲线
importData.ProcessByLevel = ;
// 测试所有级别
// allLevels = True
// False = vOnlyLev中指定的级别
// newFolderName =所有级别
// 或测试多个级别
// onlyLev(0)= 0
// onlyLev(1)= 6
// vOnlyLev = onlyLev
// newFolderName =第0层和第6层
// 或者,测试个别级别
oneLev = 25 ;
vOnlyLev = oneLev;
newFolderName = Layer + oneLev.ToString();
boolstatus = importData.SetLevels(allLevels,(vOnlyLev));
}

// 保留最后一项功能以确定添加的内容
// 如果这是一个新文档,则无法完成,所以
// 只需硬编码Origin功能的名称,
// 这是当前新零件文档中的最后一项功能
// 最好始终创建新的
// 首先文档,然后调用SldWorks :: LoadFile4
// 使用iargString来避免这个潜在的问题
if ((model!= null ))
{
lastFeature =(Feature)model.FeatureByPositionReverse( 0 );
lastFeatureName = lastFeature.Name;
}
else
{
lastFeatureName = Origin;
}
// 将此用户首选项设置为true表示IGES
// 对话框显示
// 将此用户首选项设置为false意味着
// < span class =code-comment>不显示IGES对话框,导入IGES数据

// ,如果不是,则使用
// 然后使用对话框的默认值
orgSetting = swApp.GetUserPreferenceToggle(( int )swUserPreferenceToggle_e.swIGESImportShowLevel);
swApp.SetUserPreferenceToggle(( int )swUserPreferenceToggle_e.swIGESImportShowLevel, false );
model =(ModelDoc2)swApp.LoadFile4(fileName,argString,importData, ref Err);


// swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swIGESImportShowLevel,orgSetting
// 如果SldWorks :: LoadFile4失败,请不要继续
< span class =code-keyword> if
(model == null
{
Debug.Print( 加载文件时出现问题。);
return ;
}
// 检索所有已创建的功能
// 并将它们移动到自己的新文件夹中
model.ClearSelection2();
// 选择FeatureManager使用的功能:: InsertFeatureTreeFolder2
< span class =code-comment> //
任何一种选择方法似乎花费相同的时间
boolstatus = select_new_features_individually(model ,lastFeatureName);
// boolstatus = multiselect_new_features(model,lastFeatureName);
if ((boolstatus))
{
newFolder = model.FeatureManager.InsertFeatureTreeFolder2(( int )swFeatureTreeFolderType_e.swFeatureTreeFolder_Containing);
if ((newFolder!= null ))
{
newFolder.Name = newFolderName;
}
model.ClearSelection2( true );
}
}

私人 bool select_new_features_individually( ModelDoc2模型, string lastFeatureName)
{
bool functionReturnValue = ;
功能testFeature = 默认(功能);
int loopCount = 0 ;
bool boolstatus = false ;
functionReturnValue = false ;
loopCount = 0 ;
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
while (((testFeature!= null ))&&(!(testFeature) .Name == lastFeatureName)))
{
loopCount = loopCount + 1 ;
boolstatus = testFeature.Select2( true 0 );
if (!(boolstatus = false ))
{
functionReturnValue = true ;
}
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
}
return functionReturnValue;
}

private bool multiselect_new_features(ModelDoc2 model, string lastFeatureName)
{
bool functionReturnValue = ;
功能testFeature = 默认(功能);
int loopCount = 0 ;
功能[] featureList = null ;
object vFeatureList = null ;
int longstatus = 0 ;
SelectData selData = 默认(SelectData);
SelectionMgr selMgr = 默认(SelectionMgr);
functionReturnValue = false ;
loopCount = 0 ;
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
while (((testFeature!= null ))&&(!(testFeature) .Name == lastFeatureName)))
{
loopCount = loopCount + 1 ;
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
}
Array.Resize( ref featureList,(loopCount + 1 )) ;

loopCount = 0 ;
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
while (((testFeature!= null ))&&(!(testFeature) .Name == lastFeatureName)))
{
featureList [loopCount] = testFeature;
loopCount = loopCount + 1 ;
testFeature =(Feature)model.FeatureByPositionReverse(loopCount);
}

vFeatureList = featureList;
selMgr =(SelectionMgr)model.SelectionManager;
selData =(SelectData)selMgr.CreateSelectData();
longstatus = model.Extension.MultiSelect2((vFeatureList), true ,selData);
if (longstatus > 0
{
functionReturnValue = true ;
}
return functionReturnValue;
}

/// < 摘要 >
/// 为您预先分配了SldWorks swApp变量。
/// < / summary >
public SldWorks swApp;

}
}





请帮助我

真诚的你的

解决方案

Visual Studio 2010和Autocad导入 [ ^ ]

Hi Dears
I want to import iges files (CAD files) into the C#.
But i dont know what i should do.
I found this Codes But I confused.

This example shows how to:

specify levels and values for importing IGES data.
import an IGES file.
//------------------------------------------------
// Preconditions: Substitute the path and name
// of your IGES file where noted in the code.
//
// Postconditions: 
// 1. Creates a folder named Layer 25.
// 2. Imports the specified IGES file into SolidWorks
//    and moves the imported IGES features to Layer 25.
// 3. To verify, examine the graphics area and
//    FeatureManager design tree.
//--------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace ImportIGESFilesCSharp.csproj
{
    public partial class SolidWorksMacro
    {
        public void Main()
        {
 
            ModelDoc2 model = default(ModelDoc2);
            bool boolstatus = false;
            string fileName = null;
            string argString = null;
            ImportIgesData importData = default(ImportIgesData);
            int Err = 0;
            bool orgSetting = false;
            bool allLevels = false;
            object vOnlyLev = null;
            int[] onlyLev = new int[2];
            int oneLev = 0;
            Feature lastFeature = default(Feature);
            Feature newFolder = default(Feature);
            string newFolderName = "";
            string lastFeatureName = null;
 
 
            model = (ModelDoc2)swApp.ActiveDoc;
            // Substitute the path and name of your IGES file
            fileName = "C:\\beam_with_holes.igs";
            // "r" means open new document
            // "i" means insert into existing document
            if (model == null)
            {
                argString = "r";
                // There is an existing part, so use it
            }
            else
            {
                argString = "i";
            }
            // Fill in the import data
            importData = (ImportIgesData)swApp.GetImportFileData(fileName);
            if ((importData != null))
            {
                // Test the various flags
                importData.IncludeSurfaces = true;
                importData.IncludeCurves = true;
                importData.CurvesAsSketches = true;
                // False = Curves as Curves                 
                importData.ProcessByLevel = false;
                // Test all levels
                //        allLevels = True
                // False = levels specified in vOnlyLev
                //        newFolderName = "All levels"
                // or, test multiple levels
                //        onlyLev(0) = 0
                //        onlyLev(1) = 6
                //        vOnlyLev = onlyLev
                //        newFolderName = "Layer 0 and 6"
                // Or, test individual levels
                oneLev = 25;
                vOnlyLev = oneLev;
                newFolderName = "Layer " + oneLev.ToString();
                boolstatus = importData.SetLevels(allLevels, (vOnlyLev));
            }
 
            // Keep the last feature to determine what's been added
            //   If this is a new document, that cannot be done, so
            //   just hard code the name of the Origin feature,
            //   which is currently the last feature in a new part document
            //   It is better to always create a new
            //   document first, and then call SldWorks::LoadFile4
            //   with "i" argString to avoid this potential problem
            if ((model != null))
            {
                lastFeature = (Feature)model.FeatureByPositionReverse(0);
                lastFeatureName = lastFeature.Name;
            }
            else
            {
                lastFeatureName = "Origin";
            }
            // Setting this user preference to true means that the IGES
            //   dialog is displayed
            // Setting this user preference to false means that the
            //   IGES dialog is not displayed, and the import IGES data
            //   is used if it is passed in, or, if it is not,
            //   then the default values for the dialog are used
            orgSetting = swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swIGESImportShowLevel);
            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swIGESImportShowLevel, false);
            model = (ModelDoc2)swApp.LoadFile4(fileName, argString, importData, ref Err);
 
 
            // swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swIGESImportShowLevel, orgSetting
            // If the SldWorks::LoadFile4 failed, do not continue
            if (model == null)
            {
                Debug.Print("Problem loading file.");
                return;
            }
            // Retrieve all of the features that were created
            // and move them into their own new folder
            model.ClearSelection2(true);
            // Select features that are then used by FeatureManager::InsertFeatureTreeFolder2
            // Either method of selection seems to take the same amount of time
            boolstatus = select_new_features_individually(model, lastFeatureName);
            // boolstatus = multiselect_new_features(model, lastFeatureName);
            if ((boolstatus))
            {
                newFolder = model.FeatureManager.InsertFeatureTreeFolder2((int)swFeatureTreeFolderType_e.swFeatureTreeFolder_Containing);
                if ((newFolder != null))
                {
                    newFolder.Name = newFolderName;
                }
                model.ClearSelection2(true);
            }
        }
 
        private bool select_new_features_individually(ModelDoc2 model, string lastFeatureName)
        {
            bool functionReturnValue = false;
            Feature testFeature = default(Feature);
            int loopCount = 0;
            bool boolstatus = false;
            functionReturnValue = false;
            loopCount = 0;
            testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            while (((testFeature != null)) && (!(testFeature.Name == lastFeatureName)))
            {
                loopCount = loopCount + 1;
                boolstatus = testFeature.Select2(true, 0);
                if (!(boolstatus = false))
                {
                    functionReturnValue = true;
                }
                testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            }
            return functionReturnValue;
        }
 
        private bool multiselect_new_features(ModelDoc2 model, string lastFeatureName)
        {
            bool functionReturnValue = false;
            Feature testFeature = default(Feature);
            int loopCount = 0;
            Feature[] featureList = null;
            object vFeatureList = null;
            int longstatus = 0;
            SelectData selData = default(SelectData);
            SelectionMgr selMgr = default(SelectionMgr);
            functionReturnValue = false;
            loopCount = 0;
            testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            while (((testFeature != null)) && (!(testFeature.Name == lastFeatureName)))
            {
                loopCount = loopCount + 1;
                testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            }
            Array.Resize(ref featureList, (loopCount + 1));
 
            loopCount = 0;
            testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            while (((testFeature != null)) && (!(testFeature.Name == lastFeatureName)))
            {
                featureList[loopCount] = testFeature;
                loopCount = loopCount + 1;
                testFeature = (Feature)model.FeatureByPositionReverse(loopCount);
            }
 
            vFeatureList = featureList;
            selMgr = (SelectionMgr)model.SelectionManager;
            selData = (SelectData)selMgr.CreateSelectData();
            longstatus = model.Extension.MultiSelect2((vFeatureList), true, selData);
            if (longstatus > 0)
            {
                functionReturnValue = true;
            }
            return functionReturnValue;
        }
 
        /// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        public SldWorks swApp;
 
    }
}



Please help me
sincerely yours

解决方案

Visual Studio 2010 and Autocad Import[^]


这篇关于如何将.iges文件导入c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆