EPDM GetObject: How do I get a IEdmObject5 object without Specific Type

I have a Before Checkin Event in my Addin.  As I cycle through the ppoData, I need to get the fileobject like this

 

ThisFile = poCmd.mpoVault.GetObject(EdmObjectType.EdmObject_File, FileUID)

Which is all good and well if I know that it is a FILE (EdmObjectType.EdmObject_File)

What if the item turns out to be a BOM?  I will get an error.

I need to get the iEdmObject5 generic object, get it's type and then decide whether to ignore it or not.

Bottom Line:  I have an ID.  How do I get the object type?

Any ideas. 

(I could use a series of try catch blocks, but I hate that approach)

SolidworksApi macros
Answers 12
WM
WM 2011-09-08 Best Answer

Can you check the name of the file,  ppoData(i).mbsStrData1 if extension is BOM then it is a BOM.

LY
LY 2011-09-08

As everything inherits from IEdmObject5, simply cast your object to IEdmObject5.

IEdmObject5 obj = (IEdmObject5)file;

KK
KK 2011-09-08

But you can't get the IEdmObject5 object without knowing the type first

dim thisObject as IEdmObject 5 = thisVault.GetObject(EdmObjectType.EdmObject_File, FileUID)

Now, if I could to this, it would be great

dim thisObject as IEdmObject 5 = thisVault.GetObject(FileUID)

KK
KK 2011-09-08

No, they all pass the original filename, not the real database extension

The following object is actually a BOM:

DirectCast(ppoData,EdmLib.EdmCmdData())(1).mbsStrData1 "C:\WestportEPDM_Test\Test\Resonator\887766-90.SLDPRT"

LY
LY 2011-09-09

Aye, you're correct and I guess I missed the whole point of the post. I apologise.

The issue you're going to run into is that the ID of the object can be duplicated. One can have a folder and file with both an ID of 1. (And all vaults do.)

You're going to need to do some fancy error handling and create a helper method that accepts and returns the type you're looking for. If you're stuck let me know and I'll try to whip something up.

<p>public static IEdmObject5 GetObject(IEdmVault12 vault, int objectID, EdmObjectType objectType)</p><p>{</p><p>    try</p><p>    {</p><p>        IEdmObject5 obj = vault.GetObject(objectType, objectID);                </p><p>        return obj;</p><p>    }</p><p>    catch (Exception)</p><p>    {</p><p>        //swallow it</p><p>    }</p><p>    foreach (EdmObjectType enumObjectType in Enum.GetValues(typeof(EdmObjectType)))</p><p>    {</p><p>        try</p><p>        {</p><p>            IEdmObject5 obj = vault.GetObject(enumObjectType, objectID);</p><p>            return obj;</p><p>        }</p><p>        catch (Exception)</p><p>        {</p><p>            //swallow it again</p><p>        }</p><p>    }</p><p>    //nothing found</p><p>    return null;</p><p>}</p><p></p><p></p><p>IEdmObject5 obj =  EPDM.Utils.DataHelper.GetObject(vault, workingFile.mlObjectID1, EdmObjectType.EdmObject_Folder);</p><p>if (obj.ObjectType == EdmObjectType.EdmObject_Folder)</p><p>{</p><p>    //do something.</p><p>}</p>
WM
WM 2011-09-09

Can you use the method IEdmVault5::GetFileFromPath passing the ppoData,EdmLib.EdmCmdData())(1).mbsStrData1 as filename? This method returns a File object rather than a generic EdmObject.

KK
KK 2011-09-09

My database declares the documentID as the key identity and therefore cannot be duplicated.  Do you mean that a Project.projectID can duplicate the Documents.DocumentID?  If the Documents.DocumentID is unique, you should be able to write a simple function to retreive a unique object.  I can do this with SQL easy enough.

declare @ID int

set @ID = 235521

select d.DocumentID, d.FileName, o.Description

from documents d

inner join objecttype o on d.ObjectTypeID = o.ObjectTypeID

where DocumentID = @ID

So, why is this so difficult in EPDM API?

KK
KK 2011-09-09

True, but then my part with cutlist will send a separate array item for each cutlist item.  Each will have the same filename, so I will end up running my code through more loops than necessary.

My function runs before the checkin event.  It checks to see if a variable is set to a certain value.  (if External Review = 1 then alert the user and abort the checkin).

This is only valid for files, not BOM entries.  But the ppoData array contain these objects as well, with no way for the program to ignore them.  I ended up using a try catch block and it works.  But I hate over using this.  I don't like throwing errors, even if I handle them.  It seems to me that clean codes should not rely on error handling.  Maybe I'm wrong about this.

I'm just confused as to why there is no function that returns a IEdmObject5 object without knowning in advance the type of object it is (like my simple SQL statement can do)

LY
LY 2011-09-09

The API doesn't know what objectType you want when you ask for an object with an ID of say 40. Folders/Projects and Documents can have the same ID.

The reason is API doesn't have a GetObject method with one parameter specifing the ID is because it needs to know what database table to look at as well. Your query specifies a table, EdmObjectType does the same thing.

KK
KK 2011-09-09

I understand completely what you are saying.  Unfortunately

"Folders" are actually "Projects" and are found in the projects table and indeed have different IDs.  Users are found in the user table and have different IDs.

However, EdmObject_File and EdmObject_BOM share a table (documents) and are returned together by the ppoData array (or EdmCmdData).  There is no way to get the object and make a decision between them in the middle of your code loop. 

At the end of the day, the ppoData array should have a data field that show what type of object it represents.  If ID is completely ambiguous, like you and I agree, then more data is needed.

mlObjectID1

long

ID of file to check in.

mlObjectID2

long

ID of the file's parent folder.

mbsStrData1

BSTR

Path to the file.

is not enough information, since Files and BOMs will have identicle information except for the ID

Kent

WM
WM 2011-09-09

Sorry for taking so long to understand what was going on.

Other solution would be that weldment parts with cutlist items only have one element in the ppoData array for each part. You cannot check in the part without the cutlist items or a cutlist item without the part.

LY
LY 2011-09-09

I agree that the EdmCmdData struct should have a member for the object type. Wouldn't hurt.

However, with the method that I posted one can tell what the object is and make a decision. Like you said, Files and BOMs (items too?) have the same information except for the ID, which is enough because you know what you're looking for. (IMO)

Anywho, looks like you got it sorted out. I'd make an enhancement request for the extra member in the EdmCmdData struct.