Removing components from "internal" memory

Removing components from "internal" memory

nedeljko.sovljanski
Advocate Advocate
1,083 Views
3 Replies
Message 1 of 4

Removing components from "internal" memory

nedeljko.sovljanski
Advocate
Advocate

I used this example from Inventor API help

 

Public Sub CopyBodyFromPartToPart()
    ' The first portion of this program creates an assembly so that the
    ' copy body API can be demonstrated. Any assembly could potentially
    ' be used, but the sample is simpler if it's for a specific case.
    '
    ' This creates an assembly that contains two parts. An interesting
    ' aspect of the sample is that the assembly and parts only exist
    ' in memory and are not written to disk. Filenames are assigned,
    ' so that if you perform a Save they will be written to disk using
    ' the specified filenames.

    ' Create a new assembly.
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.Documents.Add(kAssemblyDocumentObject, ThisApplication.FileManager.GetTemplateFile(kAssemblyDocumentObject))

    ' Define the filename for the assembly. It won't be saved at this point, but
    ' this name will be used if the assembly is saved later by the user.
    oAsmDoc.FullFileName = "C:\Users\TCuser\Documents\Inventor\Kompozitor\CopyFaces\CopyBodyTestAsm.iam"

    ' Create a new part, invisibly.
    Dim oPartDoc1 As PartDocument
    Set oPartDoc1 = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject), False)

    ' Define the filename for the part. It won't be saved at this point, but
    ' this name will be used if the part is saved later by the user.
    oPartDoc1.FullFileName = "C:\Users\TCuser\Documents\Inventor\Kompozitor\CopyFaces\CopyBodyTestPart1.ipt"

    ' Set a reference to the transient geometry object.
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry

    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc1.ComponentDefinition
    
    ' Create an extrude feature.
    Dim oSketch As PlanarSketch
    Set oSketch = oCompDef.Sketches.Add(oCompDef.WorkPlanes.Item(1))
    Call oSketch.SketchCircles.AddByCenterRadius(oTG.CreatePoint2d(0, 0), 2)
    Dim oProfile As Profile
    Set oProfile = oSketch.Profiles.AddForSolid
    Dim oExtrudeDef As ExtrudeDefinition
    Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
    Call oExtrudeDef.SetDistanceExtent(3, kPositiveExtentDirection)
    Dim oExtrude As ExtrudeFeature
    Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)
    
    ' Create a second extrude feature as a work surface.
    Set oSketch = oPartDoc1.ComponentDefinition.Sketches.Add(oPartDoc1.ComponentDefinition.WorkPlanes.Item(1))
    Call oSketch.SketchLines.AddAsTwoPointRectangle(oTG.CreatePoint2d(-3, -3), oTG.CreatePoint2d(3, 3))
    Set oProfile = oSketch.Profiles.AddForSolid

    Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kSurfaceOperation)
    Call oExtrudeDef.SetDistanceExtent(1.5, kPositiveExtentDirection)
    Set oExtrude = oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)

    ' Insert the occurrence into the assembly using a somewhat arbitrary position.
    Dim oMatrix As Matrix
    Set oMatrix = oTG.CreateMatrix
    Call oMatrix.Translation.AddVector(oTG.CreateVector(2, 3, 4))
    Call oMatrix.SetToRotation(0.5, oTG.CreateVector(1, 0, 0), oTG.CreatePoint(2, 3, 4))
    Dim oOcc1 As ComponentOccurrence
    Set oOcc1 = oAsmDoc.ComponentDefinition.Occurrences.AddByComponentDefinition(oPartDoc1.ComponentDefinition, oMatrix)

    ' Create a second new part, invisibly.
    Dim oPartDoc2 As PartDocument
    Set oPartDoc2 = ThisApplication.Documents.Add(kPartDocumentObject, ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject), False)

    ' Define the filename for the part. It won't be saved at this point, but
    ' this name will be used if the part is saved later by the user.
    oPartDoc2.FullFileName = "C:\Users\TCuser\Documents\Inventor\Kompozitor\CopyFaces\CopyBodyTestPart2.ipt"

    ' Insert the second part into the assembly using a somewhat arbitrary position.
    Set oMatrix = oTG.CreateMatrix
    Call oMatrix.Translation.AddVector(oTG.CreateVector(-1, -1, -1))
    Call oMatrix.SetToRotation(0.5, oTG.CreateVector(1, 1, 0), oTG.CreatePoint(1, 1, 1))
    Dim oOcc2 As ComponentOccurrence
    Set oOcc2 = oAsmDoc.ComponentDefinition.Occurrences.AddByComponentDefinition(oPartDoc2.ComponentDefinition, oMatrix)

    ' Get the surface body from the first part that represents the work surface.
    ' In this case we know there's only one work surface so this just gets the
    ' first work surface in the collection.
    Dim oWorkSurface As WorkSurface
    Set oWorkSurface = oPartDoc1.ComponentDefinition.WorkSurfaces.Item(1)
    Dim oBody As SurfaceBody
    Set oBody = oWorkSurface.SurfaceBodies.Item(1)

    ' Define the matrix to use in copying the surface body from one part to
    ' another. Any matrix can be used, but in this case I want the position
    ' of the body to be the same with respect to assembly space. Because
    ' the occurrences are in different positions within the assembly the matrix
    ' needs to take into account the transfrom from one occurrence to the other.

    ' Get the transform from the occurrence where the surface body currently exists.
    ' This defines the tranform of the surface body into assembly space.
    Set oMatrix = oOcc1.Transformation

    ' Get the matrix of the second occurrence and invert it.
    ' The inverse of the second occurrence's transform defines the tranform from
    ' assembly space into that occurrence's part space.
    Dim oMatrix2 As Matrix
    Set oMatrix2 = oOcc2.Transformation
    oMatrix2.Invert

    ' Combine these matrices.
    Call oMatrix.PreMultiplyBy(oMatrix2)

    ' Copy the surface body from the first part into the second. You shouldn't
    ' see anything graphically change because the new body is directly on top of
    ' the existing body, but it can be verified because the new body is in
    ' the second part.
    'Call oPartDoc2.ComponentDefinition.Features.NonParametricBaseFeatures.Add(oBody, oMatrix)
            
    Dim oDerPartDef As DerivedPartCoordinateSystemDef
    Set oDerPartDef = oPartDoc2.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateCoordinateSystemDef(oPartDoc1.FullFileName)
    
    oDerPartDef.ExcludeAll
    oDerPartDef.IncludeAllurfaces = kDerivedIndividualDefined
    oDerPartDef.Surfaces.Item(1).IncludeEntity = True

    Call oPartDoc2.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerPartDef)
End Sub

 

First time everything is fine.I closed created assembly and deleted created files from disc. But second time subroutine stopped on

oPartDoc1.FullFileName = "C:\Users\TCuser\Documents\Inventor\Kompozitor\CopyFaces\CopyBodyTestPart1.ipt"

with message:  Run-time error'5': Invalid procedure call or argument.

 

All I need to do is to close Inventor and run again, and I have next one shot.

 

Is there any way to purge Inventor from residual files in memory?

0 Likes
Accepted solutions (2)
1,084 Views
3 Replies
Replies (3)
Message 2 of 4

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @nedeljko.sovljanski 

Try this:

ThisApplication.Documents.CloseAll(True)

To close all unreferenced files in Inventors memory 🙂

Message 3 of 4

WCrihfield
Mentor
Mentor
Accepted solution

There is another similar tool you can use that works closely with the method Joel mentioned.

oDoc.ReleaseReference

Here is the pop-up info for this Sub.

Document-ReleaseReference.png

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 4

nedeljko.sovljanski
Advocate
Advocate

Both solution can be accepted but IMPORTANT notice:

 

They must be implement on the end of Sub CopyBodyFromPartToPart().

 

first implementation is:

oPartDoc1.ReleaseReference

oPartDoc2.ReleaseReference

 

second implementation is:

ThisApplication.Documents.CloseAll True