Skip to content

Commit a4c0d8e

Browse files
pelakmvasicem
authored andcommitted
Added a new project "debugPluginLocally" (#3)
Allows to debug plugin easily using Inventor
1 parent 0911113 commit a4c0d8e

File tree

9 files changed

+307
-4
lines changed

9 files changed

+307
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
# Understanding the sample
3737
A detailed description of the sample is available in [Docs/DeveloperNotes.md](Docs/DeveloperNotes.md).
3838

39+
## How to debug samplePlugin locally
40+
41+
Make sure that Inventor is installed and its license is not expired. Set debugPluginLocally as startup project and start it. A new instance of Inventor will be launched. If there are any break points in the samplePlugin, the program execution will break.
42+
3943
## Quotas and Limits
4044
Apps, Activies and WorkItems have quotoas and limits. To find out more information on this can be found in [Docs/QuotasAndRestrictions.md](Docs/QuotasAndRestrictions.md).
4145

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
using Inventor;
5+
6+
namespace debugPluginLocally
7+
{
8+
class InventorConnector : IDisposable
9+
{
10+
Application _Instance;
11+
bool _CreatedByUs;
12+
const string PROG_ID = "Inventor.Application";
13+
14+
public InventorConnector()
15+
{
16+
}
17+
18+
public InventorServer GetInventorServer()
19+
{
20+
Connect();
21+
return _Instance as InventorServer;
22+
}
23+
24+
private void Connect()
25+
{
26+
if (_Instance == null)
27+
{
28+
_Instance = TryConnectToRunningInstance();
29+
if (_Instance == null)
30+
{
31+
_Instance = TryCreateInstance();
32+
_CreatedByUs = _Instance != null;
33+
}
34+
if (_Instance == null)
35+
throw new ApplicationException("Could not connect to Inventor.");
36+
}
37+
}
38+
39+
private static Application TryCreateInstance()
40+
{
41+
Console.WriteLine("Trying to create instance of Inventor...");
42+
Application app = null;
43+
try
44+
{
45+
Type type = Type.GetTypeFromProgID(PROG_ID);
46+
app = Activator.CreateInstance(type) as Application;
47+
Console.WriteLine($"Connected to Inventor {app.SoftwareVersion.DisplayName}");
48+
49+
// show Inventor UI
50+
app.Visible = true;
51+
}
52+
catch (Exception e)
53+
{
54+
Console.WriteLine($"No running Inventor instance... ({e.Message})");
55+
}
56+
return app;
57+
}
58+
59+
private static Application TryConnectToRunningInstance()
60+
{
61+
Console.WriteLine("Trying to connect to Inventor...");
62+
Application app = null;
63+
try
64+
{
65+
app = Marshal.GetActiveObject(PROG_ID) as Application;
66+
Console.WriteLine($"Connected to Inventor {app.SoftwareVersion.DisplayName}");
67+
}
68+
catch /*(Exception e)*/
69+
{
70+
//Console.WriteLine($"Could not connect to running Inventor Instance... ({e.Message})");
71+
}
72+
return app;
73+
}
74+
75+
public void Dispose()
76+
{
77+
if (_Instance != null)
78+
{
79+
Console.WriteLine("Closing all documents...");
80+
_Instance.Documents.CloseAll(UnreferencedOnly: false);
81+
82+
if (_CreatedByUs)
83+
{
84+
// Uncomment to close the Inventor instance
85+
//_Instance.Quit();
86+
}
87+
88+
Console.WriteLine("Detaching from Inventor...");
89+
Marshal.ReleaseComObject(_Instance);
90+
_Instance = null;
91+
}
92+
}
93+
}
94+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using Inventor;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.IO;
8+
9+
namespace debugPluginLocally
10+
{
11+
class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
using (var inv = new InventorConnector()) {
16+
InventorServer server = inv.GetInventorServer();
17+
18+
try
19+
{
20+
Console.WriteLine("Running locally...");
21+
// run the plugin
22+
DebugSamplePlugin(server);
23+
}
24+
catch(Exception e)
25+
{
26+
string message = $"Exception: {e.Message}";
27+
if (e.InnerException != null)
28+
message += $"{System.Environment.NewLine} Inner exception: {e.InnerException.Message}";
29+
30+
Console.WriteLine(message);
31+
}
32+
finally
33+
{
34+
if (System.Diagnostics.Debugger.IsAttached)
35+
{
36+
Console.WriteLine("Press any key to exit. All documents will be closed.");
37+
Console.ReadKey();
38+
}
39+
}
40+
}
41+
}
42+
43+
/// <summary>
44+
/// Opens box.ipt and runs samplePlugin
45+
/// </summary>
46+
/// <param name="app"></param>
47+
private static void DebugSamplePlugin(InventorServer app)
48+
{
49+
// get project directory
50+
string projectdir = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
51+
52+
// get box.ipt absolute path
53+
string boxPath = System.IO.Path.Combine(projectdir, @"inputFiles\", "box.ipt");
54+
55+
string boxPathCopy = System.IO.Path.Combine(projectdir, @"inputFiles\", "boxcopy.ipt");
56+
57+
try
58+
{
59+
// delete an existing file
60+
System.IO.File.Delete(boxPathCopy);
61+
}
62+
catch (IOException e)
63+
{
64+
Console.WriteLine("The specified file is in use. It might be open by Inventor");
65+
return;
66+
}
67+
68+
// create a copy
69+
System.IO.File.Copy(boxPath, boxPathCopy);
70+
71+
// open box.ipt by Inventor
72+
Document doc = app.Documents.Open(boxPathCopy);
73+
74+
// get params.json absolute path
75+
string paramsPath = System.IO.Path.Combine(projectdir, @"inputFiles\", "params.json");
76+
77+
// create a name value map
78+
Inventor.NameValueMap map = app.TransientObjects.CreateNameValueMap();
79+
80+
// add parameters into the map, do not change "_1". You may add more parameters "_2", "_3"...
81+
map.Add("_1", paramsPath);
82+
83+
// create an instance of samplePlugin
84+
samplePlugin.SampleAutomation plugin = new samplePlugin.SampleAutomation(app);
85+
86+
// run the plugin
87+
plugin.RunWithArguments(doc, map);
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("debugPluginLocally")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("debugPluginLocally")]
13+
[assembly: AssemblyCopyright("Copyright © 2018")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("96c767e6-7828-4545-a4c9-ea2346d9e17f")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{96C767E6-7828-4545-A4C9-EA2346D9E17F}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>debugPluginLocally</RootNamespace>
10+
<AssemblyName>debugPluginLocally</AssemblyName>
11+
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="Autodesk.Inventor.Interop, Version=23.0.0.0, Culture=neutral, PublicKeyToken=d84147f8b4276564, processorArchitecture=MSIL">
36+
<SpecificVersion>False</SpecificVersion>
37+
<EmbedInteropTypes>True</EmbedInteropTypes>
38+
<HintPath>..\packages\autodesk\autodesk.inventor.interop.dll</HintPath>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Xml" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="InventorConnector.cs" />
51+
<Compile Include="Program.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="App.config" />
56+
<None Include="inputFiles\box.ipt" />
57+
<None Include="inputFiles\params.json" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<ProjectReference Include="..\samplePlugin\samplePlugin.csproj">
61+
<Project>{f1c9d7e2-53a8-4e4e-af9e-931ca891715d}</Project>
62+
<Name>samplePlugin</Name>
63+
</ProjectReference>
64+
</ItemGroup>
65+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
66+
</Project>
132 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"length":"20","width":"10"}

Solution/design-automation-csharp-inventor.sln

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "samplePlugin", "samplePlugi
1515
EndProject
1616
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZipAppPackage", "ZipAppPackage\ZipAppPackage.csproj", "{F15EB857-DBB4-4CD1-827C-EC8FB6EB945E}"
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "debugPluginLocally", "debugPluginLocally\debugPluginLocally.csproj", "{96C767E6-7828-4545-A4C9-EA2346D9E17F}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
2123
Release|Any CPU = Release|Any CPU
2224
EndGlobalSection
2325
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26+
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Release|Any CPU.Build.0 = Release|Any CPU
2430
{F1C9D7E2-53A8-4E4E-AF9E-931CA891715D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2531
{F1C9D7E2-53A8-4E4E-AF9E-931CA891715D}.Debug|Any CPU.Build.0 = Debug|Any CPU
2632
{F1C9D7E2-53A8-4E4E-AF9E-931CA891715D}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -29,10 +35,10 @@ Global
2935
{F15EB857-DBB4-4CD1-827C-EC8FB6EB945E}.Debug|Any CPU.Build.0 = Debug|Any CPU
3036
{F15EB857-DBB4-4CD1-827C-EC8FB6EB945E}.Release|Any CPU.ActiveCfg = Release|Any CPU
3137
{F15EB857-DBB4-4CD1-827C-EC8FB6EB945E}.Release|Any CPU.Build.0 = Release|Any CPU
32-
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33-
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Debug|Any CPU.Build.0 = Debug|Any CPU
34-
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{C0C07CF7-75ED-4820-96C6-6A4709547B67}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{96C767E6-7828-4545-A4C9-EA2346D9E17F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{96C767E6-7828-4545-A4C9-EA2346D9E17F}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{96C767E6-7828-4545-A4C9-EA2346D9E17F}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{96C767E6-7828-4545-A4C9-EA2346D9E17F}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)