Open Dgn file:Error HRESULT E_FAIL has been returned from a call to a COM component ?

1863
4
01-22-2014 04:57 PM
TaiBui
by
Occasional Contributor II
Hi everyone,

I am trying open and read Dgn file. But, I got the error when using below codes:

 IWorkspaceFactory factory = new CadWorkspaceFactoryClass ();

 IWorkspace workspace = factory.OpenFromFile(fileName, 0); // Throw exception


Do you know why ?

I attached the dgn file.

Thanks and regards,

Tai
0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
Never use New to create an instance of a singleton object like a workspace factory.  Always use the Activator class.  Also, is this a standalone application?  If so, are you binding to a product then checking out a license before running this code?
0 Kudos
TaiBui
by
Occasional Contributor II
Never use New to create an instance of a singleton object like a workspace factory.  Always use the Activator class.  Also, is this a standalone application?  If so, are you binding to a product then checking out a license before running this code?


Thanks Neil,

I tried to use Activator, but still not successful.

Type factoryType = Type.GetTypeFromProgID(
                 "esriDataSourcesFile.CadWorkspaceFactory");
System.Object obj = Activator.CreateInstance(factoryType);

IWorkspaceFactory factory = obj as IWorkspaceFactory;
IFeatureWorkspace workspace = (IFeatureWorkspace)factory.OpenFromFile(dgnFilePath, 0);



Can I use dgn file path in the OpenFromFile method ?

Thanks and regards,
Tai
Cristian_Galindo
Occasional Contributor III

Any news?????

0 Kudos
bojko108
New Contributor II

Hello, I had the same issue with reading DWG files using custom SOE and managed to solve it. It turned out that the OpenFromFile() method must be called with directory path not the file path as argument. 

So instead of:

IWorkspace workspace = factory.OpenFromFile(fileName, 0);

Try with this:

string directoryName = fileName.Substring(0, fileName.LastIndexOf('\\'));

IWorkspace workspace = factory.OpenFromFile(directoryName, 0);
0 Kudos