API: inserting a component from a file

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

Reply to
JM
Loading thread data ...

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.

Reply to
Paul Delhanty

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

Reply to
Sean Phillips

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

Reply to
JM

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

Reply to
JM

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

formatting link

Reply to
Paul Delhanty

you are asking for somthing that you can not do.

Reply to
Sean Phillips

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

JM

Reply to
JM

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()

'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()

'close the part document PartDoc.close

I hope this helps ....

Steve

Reply to
Steve Stojanovski

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.