Copy Surface to right position in other part

Copy Surface to right position in other part

rikard.nilsson
Collaborator Collaborator
385 Views
1 Reply
Message 1 of 2

Copy Surface to right position in other part

rikard.nilsson
Collaborator
Collaborator

Hi,

 

I really can't understand this with Matrix.

In the attached zip you can find a very simple assembly with 3 parts..

 

But I can't have my copied surfaces to be placed in the right position in my CopyToOcc..

 

If my CopyToOcc has the same directions as top-assy then it works but not if I turn it..

 

Is there someone who can tell me how to code this so the surfaces will be placed in the same position and rotations as my occurrences that I copy from.

 

 

Regards

Rikard

 

 

SyntaxEditor Code Snippet

    Dim oAssemblyDef As AssemblyComponentDefinition = oAssemblyDoc.ComponentDefinition


    Dim CopyToOcc As ComponentOccurrence = Component.InventorComponent("CopyToOcc")
    Dim CopyToOccDef As PartComponentDefinition = CopyToOcc.Definition
    
    
    Dim CopyFromOcc1 As ComponentOccurrence = Component.InventorComponent("CopyFromOcc1")
    Dim CopyFromOcc1Def As PartComponentDefinition = CopyFromOcc1.Definition

    

    Dim CopyFromOcc2 As ComponentOccurrence = Component.InventorComponent("CopyFromOcc2")
    Dim CopyFromOcc2Def As PartComponentDefinition = CopyFromOcc2.Definition
        
    Dim oSourceBody As SurfaceBody = CopyFromOcc1Def.Worksurfaces.Item(1).Surfacebodies(1)
    Dim oSourceBody1 As SurfaceBody = CopyFromOcc2Def.Worksurfaces.Item(1).Surfacebodies(1)
    

    
    
    Dim CopyFromOcc1Matrix As Matrix
    CopyFromOcc1Matrix = CopyFromOcc1.Transformation
    
    Dim CopyFromOcc2Matrix As Matrix
    CopyFromOcc2Matrix = CopyFromOcc2.Transformation


    
    Dim CopyToOccMatrix As Matrix
    CopyToOccMatrix = CopyToOcc.Transformation
    CopyToOccMatrix.PreMultiplyBy(CopyFromOcc1Matrix)    
    Dim nonprmFea As NonParametricBaseFeature = CopyToOccDef.Features.NonParametricBaseFeatures.Add(oSourceBody, CopyToOccMatrix)
    
    CopyToOccMatrix = Nothing
    CopyToOccMatrix = CopyToOcc.Transformation
    CopyToOccMatrix.PreMultiplyBy(CopyFromOcc2Matrix)
    

    Dim nonprmFea1 As NonParametricBaseFeature =  CopyToOccDef.Features.NonParametricBaseFeatures.Add(oSourceBody1, CopyToOccMatrix)
    
   oAssemblyDoc.Update
0 Likes
Accepted solutions (1)
386 Views
1 Reply
Reply (1)
Message 2 of 2

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi Rikard,

 

Please find the sample code to create surface at same position and rotations.

 

Sub Main()
    Dim oAssemblyDoc As AssemblyDocument = ThisApplication.ActiveDocument

    Dim oAssemblyDef As AssemblyComponentDefinition = oAssemblyDoc.ComponentDefinition


    Dim CopyToOcc As ComponentOccurrence = Component.InventorComponent("CopyToOcc")
    Dim CopyToOccDef As PartComponentDefinition = CopyToOcc.Definition
	
	
    Dim CopyFromOcc1 As ComponentOccurrence = Component.InventorComponent("CopyFromOcc1")
    Dim CopyFromOcc1Def As PartComponentDefinition = CopyFromOcc1.Definition    

    Dim CopyFromOcc2 As ComponentOccurrence = Component.InventorComponent("CopyFromOcc2")
    Dim CopyFromOcc2Def As PartComponentDefinition = CopyFromOcc2.Definition
    	
    Dim oSourceBody As SurfaceBody = CopyFromOcc1Def.Worksurfaces.Item(1).Surfacebodies(1)
    Dim oSurfaceproxy As SurfaceBodyProxy
    CopyFromOcc1.CreateGeometryProxy(oSourceBody, oSurfaceproxy)
	
    Dim oSourceBody1 As SurfaceBody = CopyFromOcc2Def.Worksurfaces.Item(1).Surfacebodies(1)
    Dim oSurfaceproxy1 As SurfaceBodyProxy
    CopyFromOcc2.CreateGeometryProxy(oSourceBody1, oSurfaceproxy1)	

    Dim oBody1 As SurfaceBody
    oBody1 = ThisApplication.TransientBRep.Copy(oSourceBody)

    ThisApplication.TransientBRep.Transform(oBody1, oSurfaceproxy.ContainingOccurrence.Transformation)

    Dim trans1 As Matrix
    trans1 = CopyToOcc.Transformation

    trans1.Invert()

    ThisApplication.TransientBRep.Transform(oBody1, trans1)

    Dim oBody2 As SurfaceBody
    oBody2 = ThisApplication.TransientBRep.Copy(oSourceBody1)

    ThisApplication.TransientBRep.Transform(oBody2, oSurfaceproxy1.ContainingOccurrence.Transformation)

    Dim trans2 As Matrix
    trans2 = CopyToOcc.Transformation

    trans2.Invert()
	
    ThisApplication.TransientBRep.Transform(oBody2, trans2)
	
    CreateBaseFeature(CopyToOccDef, CopyToOcc, oBody1)

    CreateBaseFeature(CopyToOccDef, CopyToOcc, oBody2)
	
    oAssemblyDoc.Update
   
End Sub
	
Sub CreateBaseFeature(ByVal targetDef As PartComponentDefinition, ByVal targetOcc As ComponentOccurrence, ByVal body As SurfaceBody)

        Dim nonPrmFeatures As NonParametricBaseFeatures
        nonPrmFeatures = targetDef.Features.NonParametricBaseFeatures

        Dim featureDef As NonParametricBaseFeatureDefinition
        featureDef = nonPrmFeatures.CreateDefinition

        Dim transObjs As TransientObjects
        transObjs = ThisApplication.TransientObjects

        Dim col As ObjectCollection
        col = transObjs.CreateObjectCollection
        col.Add(body)

        featureDef.BRepEntities = col
        featureDef.OutputType = BaseFeatureOutputTypeEnum.kSurfaceOutputType
        featureDef.TargetOccurrence = targetOcc
        featureDef.IsAssociative = False

        Dim baseFeature As NonParametricBaseFeature
        baseFeature = nonPrmFeatures.AddByDefinition(featureDef)
End Sub

 

Please feel free to contact if there is any doubt.

 

If solves your problem, Click on "Accept as Solution" / Give a kudo.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network