McadComponent

Discussion in 'AutoCAD' started by Humberto Guerrero Obando, May 25, 2004.

  1. How can i edit a Part reference Attributes From a Mcadcomponent

    Thanks





    Public Sub makeStructure()

    Dim i As Long

    ' Initialize MCAD API environment

    ' Reference to Mcad Application

    Dim mCad As McadApplication

    Set mCad =
    ThisDrawing.Application.GetInterfaceObject("Mcad.Application")

    ' Reference to Ge Library

    Dim ge As GeApplication

    Set ge = ThisDrawing.Application.GetInterfaceObject("Ge.Application")



    Dim util As McadUtility

    Set util = mCad.ActiveDocument.Utility

    '=========================================

    ' Create some geometry for component views

    '=========================================

    ' Start with 2 rectangles

    Dim rec1 As AcadPolyline, rec2 As AcadPolyline

    Dim points As gePoints

    Set points = ge.Collection(gePoints)

    ' vertex points

    points.Add ge.Point(2, 2, 0)

    points.Add ge.Point(2, 5, 0)

    points.Add ge.Point(5, 5, 0)

    points.Add ge.Point(5, 2, 0)

    ' create first rectangle

    Set rec1 = ThisDrawing.ModelSpace.AddPolyline(points.AsArray)

    ' close the pline

    rec1.Closed = True

    ' clear points

    Set points = ge.Collection(gePoints)


    ' create second rectangle

    points.Add ge.Point(5, 3, 0)

    points.Add ge.Point(5, 5, 0)

    points.Add ge.Point(9, 5, 0)

    points.Add ge.Point(9, 3, 0)

    Set rec2 = ThisDrawing.ModelSpace.AddPolyline(points.AsArray)

    rec2.Closed = True





    ' Now add some circles

    Dim cir1 As AcadCircle, cir2 As AcadCircle, cir3 As AcadCircle

    Set cir1 = ThisDrawing.ModelSpace.AddCircle(ge.Point(3.5, 3.5,
    0).AsArray, 1.25)

    Set cir2 = ThisDrawing.ModelSpace.AddCircle(ge.Point(6, 4, 0).AsArray,
    0.75)

    Set cir3 = ThisDrawing.ModelSpace.AddCircle(ge.Point(8, 4, 0).AsArray,
    0.75)



    ZoomAll

    '==========================================

    ' Create some structure!

    '==========================================


    ' Assembly Manager reference

    Dim assymgr As Mcad2dAssemblyMgr

    Set assymgr = mCad.ActiveDocument.AssemblyMgr2d


    Dim matrix As GeMatrix

    Set matrix = ge.matrix



    'Root Component Definition

    Dim rootCompDef As McadComponentDefinition

    Set rootCompDef = assymgr.RootComponentDefinition



    ' Create a component definition for the top level assembly

    Dim AssyDef As McadComponentDefinition

    Set AssyDef = assymgr.AddNewComponentDefinition("ASSEMBLY")



    ' Create the component instance

    Dim Assy1 As McadComponent

    Set Assy1 = rootCompDef.AddComponent(AssyDef, matrix)


    '==========================================

    ' Prepare for AddNewComponentViewDefinition

    ' which creates a view def & instance

    '==========================================

    ' Create the comp view def & instance,

    ' both are set with object keys

    Dim AssyFrontViewDef As McadComponentViewDefinition

    Dim AssyFrontView1 As McadComponentView

    ' Create component views collection;

    ' add comp view instances to collection, or

    ' leave it empty.

    Dim addViews As McadComponentViews

    Set addViews = util.CreateCollection(mcComponentViews)



    ' Create array of object IDs to pass,

    ' objects are added to new view def & instance

    Dim objIDs() As Long



    ReDim objIDs(1)

    objIDs(0) = rec1.ObjectID

    objIDs(1) = cir1.ObjectID

    ' Create assembly the comp view def & instance

    assymgr.AddNewComponentViewDefinition Assy1, addViews, matrix,
    AssyFrontViewDef, AssyFrontView1, , , "Front"

    ' dim the new comps

    Dim Comp1Def As McadComponentDefinition

    Dim Comp2Def As McadComponentDefinition

    Dim Comp1 As McadComponent

    Dim Comp2 As McadComponent



    ' Create the CompDefs

    Set Comp1Def = assymgr.AddNewComponentDefinition("COMP1")

    Set Comp2Def = assymgr.AddNewComponentDefinition("COMP2")



    ' Create the instances

    Set Comp1 = AssyDef.AddComponent(Comp1Def, matrix)

    Set Comp2 = AssyDef.AddComponent(Comp2Def, matrix)

    Comp1.AddAttribute

    ' Create front views of each comp

    Dim Comp1FrontViewDef As McadComponentViewDefinition

    Dim Comp2FrontViewDef As McadComponentViewDefinition

    Dim Comp1FrontView As McadComponentView

    Dim Comp2FrontView As McadComponentView





    ' Set up origin matrix

    Dim origin As GePoint

    Dim xAxis As GeVector

    Set xAxis = ge.Vector

    Dim yAxis As GeVector

    Set yAxis = ge.Vector

    Dim zAxis As GeVector

    Set zAxis = ge.Vector

    Set origin = ge.Point(2, 2, 0)

    xAxis.Set origin, ge.Point(3, 2, 0)

    yAxis.Set origin, ge.Point(2, 3, 0)

    zAxis.Set origin, ge.Point(2, 2, 1)

    matrix.SetToCoordinateSystem origin, xAxis, yAxis, zAxis



    ' Create Comp1(Front) def & instance in Assembly(Front)

    assymgr.AddNewComponentViewDefinition Comp1, addViews, matrix,
    Comp1FrontViewDef, Comp1FrontView, objIDs, AssyFrontView1, "Front"



    ' new object IDs

    objIDs(0) = rec2.ObjectID

    objIDs(1) = cir2.ObjectID



    ' set matrix to origin of second rectangle

    Set origin = ge.Point(5, 3, 0)

    xAxis.Set origin, ge.Point(9, 3, 0)

    yAxis.Set origin, ge.Point(5, 5, 0)

    zAxis.Set origin, ge.Point(5, 3, 1)

    matrix.SetToCoordinateSystem origin, xAxis, yAxis, zAxis



    ' Create Comp2(Front) def & instance in Assembly(Front)

    assymgr.AddNewComponentViewDefinition Comp2, addViews, matrix,
    Comp2FrontViewDef, Comp2FrontView, objIDs, AssyFrontView1, "Front"



    ' Add another object to Comp2(Front)



    ReDim objIDs(0)

    objIDs(0) = cir3.ObjectID



    Comp2FrontView.AddEntities objIDs



    cir3.Delete



    '=========================================

    ' Insert a second instance of COMP2(Front)

    ' into ASSY(Front)

    '=========================================


    ' Add the second instance of the COMP

    Dim Comp2_2 As McadComponent

    Set Comp2_2 = AssyDef.AddComponent(Comp2Def, matrix)



    ' set up matrix for insertion, rotated 90 deg

    Set origin = ge.Point(2, 0, 0)

    xAxis.Set origin, ge.Point(2, 1, 0)

    yAxis.Set origin, ge.Point(3, 0, 0)

    zAxis.Set origin, ge.Point(2, 0, 1)

    matrix.SetToCoordinateSystem origin, xAxis, yAxis, zAxis



    ' add view

    AssyFrontViewDef.AddComponentView Comp2FrontViewDef, Comp2_2, matrix


    '=====================================

    ' Move Circles from COMP2(Front)

    ' into a Folder

    '=====================================

    Dim Folder1Def As McadFolderDefinition

    Dim Folder1 As McadFolder



    ' set matrix to center of Cir2


    Set origin = ge.Point(cir2.Center(0), cir2.Center(1), cir2.Center(2))

    xAxis.Set origin, ge.Point(cir2.Center(0) + 1, cir2.Center(1),
    cir2.Center(2))

    yAxis.Set origin, ge.Point(cir2.Center(0), cir2.Center(1) + 1,
    cir2.Center(2))

    zAxis.Set origin, ge.Point(cir2.Center(0), cir2.Center(1),
    cir2.Center(2) + 1)

    matrix.SetToCoordinateSystem origin, xAxis, yAxis, zAxis


    ReDim objIDs(0)

    objIDs(0) = cir2.ObjectID

    assymgr.AddNewFolderDefinition matrix, Folder1Def, Folder1, objIDs,
    Comp2FrontView, "Folder1"





    End Sub
     
    Humberto Guerrero Obando, May 25, 2004
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...