Type argument 'AssemblyDocument' does not inherit from or implement the constraint type 'Document'

Type argument 'AssemblyDocument' does not inherit from or implement the constraint type 'Document'

Anonymous
Not applicable
452 Views
3 Replies
Message 1 of 4

Type argument 'AssemblyDocument' does not inherit from or implement the constraint type 'Document'

Anonymous
Not applicable

Hello

 

Am I missing something, or do `AssemblyDocument`, `PartDocument` and `DrawingDocument` really not inherit from `Document`?

 

I'm trying to implement a wrapper class for each, with an abstract class underlying them. But when I try to implement the document type generically, I'm faced with this intellisense error, that these aren't actually related classes.

 

Two alternatives I've tried: not using a type constraint in the abstract class, but then errors are being reported about certain properties/methods not being defined; and using a set of type constraints of each document type, but then errors are reported about overload resolutions failing for certain properties/methods due to identical implementation across all.

 

Assuming all this to be true, has anyone found a clever way around this?

0 Likes
453 Views
3 Replies
Replies (3)
Message 2 of 4

Michael.Navara
Advisor
Advisor

I'm not certain I understand what do you want to do. What is the expected benefit of generic class?

Can you post some pseudo-code with description what do you try?

 

If you want to access some property from both part and assembly, you can define your own wrapper class. Or you can define some extension methods to get something useful.

 

C# example

class MyClass
{
    private void SampleUsage()
    {
        PartDocument part = null; //Initialization required
        AssemblyDocument asm = null; //Initialization required

        //Document wrapper
        var partModelDocument = new ModelDocument(part);
        var massProperties1 = partModelDocument.MassProperties;

        var asmModelDocument = new ModelDocument(asm);
        var massProperties3 = asmModelDocument.MassProperties;

        //Extension methods
        var massProperties2 = part.MassProperties();
        var massProperties4 = asm.MassProperties();
    }
    
}

class ModelDocument
{
    private readonly AssemblyDocument asm;
    private readonly PartDocument part;

    public ModelDocument(PartDocument part)
    {
        this.part = part;
    }

    public ModelDocument(AssemblyDocument asm)
    {
        this.asm = asm;
    }

    public MassProperties MassProperties => part?.ComponentDefinition.MassProperties ??
                                             asm?.ComponentDefinition.MassProperties;
}

public static class DocumentExtensions
{
    public static MassProperties MassProperties(this PartDocument part) => part.ComponentDefinition.MassProperties;
    public static MassProperties MassProperties(this AssemblyDocument asm) => asm.ComponentDefinition.MassProperties;
}

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

I can understand your approach.

I guess what I was trying to do was something like the following, which would allow me to define properties/methods agnostic to the particular document type, implementing dedicated wrappers for each which share these, and enabling more specific properties accessible only in the specific types:

 

 

Public MustInherit Class _DocWrapper(Of D As Document)
    Public ReadOnly oDoc As D = Nothing
    Public Sub New(ByRef oDoc As D)
        Me.oDoc = oDoc
    End Sub
End Class

Public Class PartDocWrapper : Inherits _DocWrapper(PartDocument)
    Public Sub SomeSub()
        Dim oCompDef As PartComponentDefinition = oDoc.ComponentDefinition
    End Sub
    Public Sub New(ByRef oDoc As PartDocument)
        MyBase.New(oDoc)
    End Sub
End Class

 

 

0 Likes
Message 4 of 4

Michael.Navara
Advisor
Advisor

In my opinion the Extension methods is the right way. Here is another few samples in VB.NET

 

 

    Sub Main()
        Dim part As PartDocument
        Dim asm As AssemblyDocument
        Dim drw As DrawingDocument

        part.SomeSub()

        Dim massProperties1 As MassProperties = part.MassProperties()
        Dim massProperties2 As MassProperties = asm.MassProperties()


        Dim partNumber1 As String = part.PartNumber
        Dim partNumber2 As String = asm.PartNumber
        Dim partNumber3 As String = drw.PartNumber

        Dim partNumber4 As String = drw.ModelPartNumber
    End Sub


Module DocumentExtensions

    <Extension>
    Public Sub SomeSub(part As PartDocument)
        Dim partComponentDefinition As PartComponentDefinition = part.ComponentDefinition
        ' Do something useful
    End Sub

    <Extension>
    Public Function MassProperties(part As PartDocument) As MassProperties
        Return part.ComponentDefinition.MassProperties
    End Function

    <Extension>
    Public Function MassProperties(asm As AssemblyDocument) As MassProperties
        Return asm.ComponentDefinition.MassProperties
    End Function


    <Extension>
    Public Function PartNumber(part As PartDocument) As String
        Return GetPartNumber(part)
    End Function

    <Extension>
    Public Function PartNumber(asm As AssemblyDocument) As String
        Return GetPartNumber(asm)
    End Function

    <Extension>
    Public Function PartNumber(drw As DrawingDocument) As String
        Return GetPartNumber(drw)
    End Function

    <Extension>
    Public Function ModelPartNumber(drw As DrawingDocument) As String
        Return GetPartNumber(drw.AllReferencedDocuments(1))
    End Function


    Private Function GetPartNumber(doc As Document) As String
        Return doc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value
    End Function

End Module

 

Don't forgot to add reference to

Imports System.Runtime.CompilerServices

 

0 Likes