autodesk inventor API – C# 二次開發

最近因為使用到autodesk inventor API 進行匯出的動作

本範例應用autodesk inventor 2020製作

autodesk inventor API 要使用基本上都要把autodesk inventor 打開才能進行

所以在使用時,請確認電腦有安裝 “autodesk inventor”與授權 !!!

先設定全域變數


private const string CLSID = "Inventor.Application";
private Inventor.DrawingDocument m_idw = null;
private Inventor.DrawingView m_baseView = null;
private Inventor.DrawingView m_projectedView = null;
private Inventor.Application m_application;
private AssemblyDocument m_mainAssembly;
private readonly string[] AddInsToLoad = { "Translator: DXF", "Translator: PDF" };
private bool m_automating = false;
private readonly string PROJECT_FULLPATH_FILENAME = @"D:\pjt\專案名稱.ipj";

開啟autodesk inventor

m_application = InitializeInventor(true, true);//啟動程式
/*查看是否已啟動inventor*/
private void CleanupRunningInstances(bool killInventors)
{
	System.Diagnostics.Process[] processes = null;
	if (killInventors)
	{
		processes = System.Diagnostics.Process.GetProcessesByName("Inventor");
		if (null != processes)
		{
			foreach (System.Diagnostics.Process p in processes)
			{
				p.Kill();
			}
		}
	}
	processes = System.Diagnostics.Process.GetProcessesByName("senddmp");
	if (null != processes)
	{
		foreach (System.Diagnostics.Process p in processes)
		{
			p.Kill();
		}
	}
}

/*啟動Inventor*/
private Inventor.Application InitializeInventor(bool bindToRunningInstance, bool makeVisible)
{
	CleanupRunningInstances(!bindToRunningInstance);
	Inventor.Application application = null;
	if (bindToRunningInstance)
	{
		// Try to get running instance
		try
		{
			application = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
		}
		catch (System.Exception ex)
		{
			System.Diagnostics.Debug.WriteLine(ex.Message);
		}
	}

	// no running instance was found or if you want a new instance, create one
	if (null == application)
	{
		try
		{
			System.Type inventorType = System.Type.GetTypeFromProgID(CLSID);
			if (null == inventorType)
			{

				// Cannot find registered typeID in the registery for Inventor
				return null;
			}
			application = (Inventor.Application)Activator.CreateInstance(inventorType);
		}
		catch (System.Exception ex)
		{
			System.Diagnostics.Debug.WriteLine(ex.Message);
		}
	}
	if (null != application)
	{
		application.Visible = makeVisible;
	}
	return application;
}

為了不影響API與目前程式產生衝突,建議關閉全部的inventor分頁

private void CloseAllDocuments()
{
	try
	{/*不存檔關閉*/
		m_application.Documents.CloseAll(false);
	}
	catch
	{
	}
}

開啟專案(*.ipj)

private void btnLoadProjectFile_Click(object sender, EventArgs e)
{
	DesignProjectManager dpm = m_application.DesignProjectManager;

	if (System.IO.Path.GetFullPath(dpm.ActiveDesignProject.FullFileName).ToLower() == System.IO.Path.GetFullPath(PROJECT_FULLPATH_FILENAME).ToLower())
	{
		// no need to do anything
		return;
	}

	// is it in the list already?
	// if not, create a shortcut to it
	IEnumerable<DesignProject> designProjects = dpm.DesignProjects.Cast<DesignProject>();
	DesignProject dp = designProjects.FirstOrDefault(proj => !string.IsNullOrEmpty(PROJECT_FULLPATH_FILENAME) &&
		proj.FullFileName.ToLower().Equals(PROJECT_FULLPATH_FILENAME.ToLower()));

	if (null == dp)
	{
		// must create a shortcut to it
		Inventor.FileOptions fo = m_application.FileOptions;

		string fileName = System.IO.Path.GetFileName(PROJECT_FULLPATH_FILENAME);
		string destPath = System.IO.Path.Combine(fo.ProjectsPath, fileName);
		Utilities.CreateShortcut(PROJECT_FULLPATH_FILENAME, destPath, "exe");

		dp = dpm.DesignProjects.ItemByName[PROJECT_FULLPATH_FILENAME];
	}

	dp.Activate();
}

匯出成圖檔 -1  (開啟->匯出(另存)->關閉)

Inventor.DrawingDocument m_file = m_application.Documents.Open("檔案路徑") as DrawingDocument;
m_file.Dirty = false; 
m_application.SilentOperation = true;//不跳出訊息
m_application.ActiveView.Fit(true); //最好的觀看大小
m_application.ActiveView.Width = width;//寬
m_application.ActiveView.Height = height;//高
m_file.SaveAs("另存的檔名&路徑", true);//匯出一率覆蓋
m_file.Close(true);//強制關閉不存檔

匯出成圖檔 -2 


private void btnSaveIDWasJPG_Click(object sender, EventArgs e)
{
	string fullFileName = GetFullSavePathAndDeleteIfExists("檔案名稱", "jpg");
	m_idw.SaveAs(fullFileName, true);
}
private string GetFullSavePathAndDeleteIfExists(string fileName, string extension)
{
	string fullFileName = System.IO.Path.Combine(FILE_PATH, fileName);
	fullFileName = System.IO.Path.ChangeExtension(fullFileName, extension);

	if (System.IO.File.Exists(fullFileName))
	{
		System.IO.File.Delete(fullFileName);
	}

	return fullFileName;
}

關閉inventor

public void closeInventor()
{
	/*關閉所有分頁+關閉程式*/
	CloseAllDocuments();
	m_application.Quit();
}

以上程式碼另增加以下程式做搭配

class Utilities
{

	public static void CreateShortcut(string fullSourceFile, string fullDestFile, string applicationFullNameWithExtension)
	{
		//
		// must add a reference to IWshRuntimeLibrary
		//(aka.  Windows Script Host Object Model)
		//
		string destDir = System.IO.Path.GetDirectoryName(fullDestFile);

		IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
		IWshRuntimeLibrary.IWshShortcut sc = shell.CreateShortcut(fullDestFile + ".lnk") as IWshRuntimeLibrary.IWshShortcut;
		sc.WindowStyle = 1;
		sc.TargetPath = fullSourceFile;
		sc.WorkingDirectory = destDir;
		sc.Description = string.Empty;
		sc.IconLocation = string.Format("{0},0", applicationFullNameWithExtension);
		sc.Save();
	}

	public static decimal ConvertToRadians(decimal angle)
	{
		return (decimal)(Math.PI / 180) * (decimal)angle;
	}


}

在〈“autodesk inventor API – C# 二次開發”〉中有 1 則留言

留言功能已關閉。