API: inserting a component from a file

Discussion in 'SolidWorks' started by JM, Dec 8, 2003.

  1. JM

    JM Guest

    Hello,

    I was experimenting a bit with the api (using VB), trying to insert a
    component to an assembly & then mate it to another component.
    But it got all a bit confusing as I found there are seemingly various ways.
    (& I'm quite a newbie to it)

    The simplest way I thought to be was using ".addcomponent4 ( compName,
    configName, x, y, z)"
    but this method requires a part already to be loaded into memory & that's
    where I got stuck.

    I tried opening it with ".OpenDoc6" & it's various options, but then I get
    the particular part opened in a part document.
    Is there a way to "load" a component without actually opening it in a
    separate document then ?

    Could anybody lighten me up a bit on this, or on which method is actually
    best used to insert a component from a file ?

    TIA & best regards,
    JM
     
    JM, Dec 8, 2003
    #1
  2. Try using AssemblyDoc.AddComponents, which adds multiple components. There
    is no mention in the API documentation that this function needs the parts to
    be loaded first. Furthermore, I just tried it out and it works for me
    without the parts being loaded first. There is an example in the API
    documentation - Add Components Example (VB) - that you can modify.
     
    Paul Delhanty, Dec 8, 2003
    #2
  3. open the part with OpenDoc6 and make sure you use
    swOpenDocOptions_Silent option. then insert it into your assembly with
    addcomponent4. after that close the doc with ModelDoc2::Close.

    hope this helps
     
    Sean Phillips, Dec 8, 2003
    #3
  4. JM

    JM Guest

    Hi Paul, thanks for your answer.
    I had been looking at this example before, but its realy 'general', more
    like a description then an example.
    E.g. where does "txtCompCount.Text" come from ?
    I' try to strip the example leaving the bare necessities.

    Best regards
    JM
     
    JM, Dec 8, 2003
    #4
  5. JM

    JM Guest

    Hi Sean, thanks for your suggestion,
    I tried that option, but it doesn't seem to behave 'silently'
    As a matter of fact, the part to be inserted does get opened in a window &
    gets focus before it gets inserted into the assembly. Unless I'm looking
    over something....

    Best regards
    JM
     
    JM, Dec 8, 2003
    #5
  6. If it helps, here is my stripped version:
    ---------------------------------------------
    Dim swApp As Object
    Dim Part As Object
    Dim title As String
    Dim compNames(0) As String
    Dim compXforms(15) As Double
    Dim vcompNames As Variant
    Dim vcompXforms As Variant
    Dim vcomponents As Variant

    Sub main()

    compNames(0) = "D:\temp\block.SLDPRT"
    ' The first 9 elements are the 3x3 rotational sub-matrix
    ' Set these to the identity rotation.
    compXforms(0) = 1
    compXforms(1) = 0
    compXforms(2) = 0
    compXforms(3) = 0
    compXforms(4) = 1
    compXforms(5) = 0
    compXforms(6) = 0
    compXforms(7) = 0
    compXforms(8) = 1
    ' The next 3 elements are the translation vector
    ' Set these to the zero translation
    compXforms(9) = 0
    compXforms(10) = 0
    compXforms(11) = 0
    ' The next element is the scaling factor.
    ' Set this to one.
    compXforms(12) = 1
    ' The next 3 elements are unused.
    ' Set these to zero to be on the safe side.
    compXforms(13) = 0
    compXforms(14) = 0
    compXforms(15) = 0
    vcompNames = compNames
    vcompXforms = compXforms

    Set swApp = Application.SldWorks

    Set Part = swApp.NewDocument("D:\Program Files\SolidWorks
    2001Plus\data\templates\Assembly.asmdot", 0, 0#, 0#)
    title = Part.GetTitle
    Set Part = swApp.ActivateDoc(title)

    vcomponents = Part.AddComponents((vcompNames), (vcompXforms))

    End Sub
    ----------------------------------------

    Paul Delhanty
    Cadcambridge Ltd
    SolidWorks Research Partner
    http://www.cadcambridge.net
     
    Paul Delhanty, Dec 8, 2003
    #6
  7. you are asking for somthing that you can not do.

     
    Sean Phillips, Dec 9, 2003
    #7
  8. JM

    JM Guest

    If it helps, here is my stripped version:


    Hi Paul,
    This works perfectly, thanks very much !!

    JM
     
    JM, Dec 9, 2003
    #8
  9. Actually, you can accomplish what you want to do. Open the document
    using the OpenDoc6 method. Then after opening, use the ModelDoc
    object that was returned by the opendoc method and set the "Visible"
    property to false. Then you can activate the assembly document again
    and then insert the component. So something like this may work:

    Dim AssyDoc as ModelDoc2
    Dim PartDoc as ModelDoc2
    Dim swApp as sldworks.sldworks
    Dim NewComp as component2

    Set swapp = CreateObject("SolidWorks.Application")

    'Get the current doc which should be the assy
    Set AssyDoc = swapp.ActiveDoc

    'Open the part document to insert into the assy. This should return a
    pointer
    'to the new part
    Set PartDoc = swApp.OpenDoc6(<insert your parameters here>)

    'Hide the part doc
    PartDoc.visible = False

    'Make the assy the active document
    swapp.ActivateDoc2 AssyDoc.GetTitle, ....

    'Insert the part into the assy
    Set NewComp = Assydoc.AddComponent4(<insert your parameters here>)

    'close the part document
    PartDoc.close

    I hope this helps ....

    Steve
     
    Steve Stojanovski, Dec 9, 2003
    #9
  10. JM

    JM Guest


    Hi Steve, thanks, but this way the part document still pops up before u turn
    off its visibility.
    however I did find a way:

    "void SldWorks.DocumentVisible ( visible, type)"
    "This method is a flag that allows you to display or hide documents as they
    are loaded."

    Setting this flag to false (for part documents) before using the opendoc6
    (don't forget closing the part & setting the flag back to true afterwards
    ofcourse)

    best regards,
    JM
     
    JM, Dec 17, 2003
    #10
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.