Notifications
Clear all

[Closed] Attach multiple Splines by Object Color

Hi Guys,

I’m having some Issues with importing several DWG files from our customer.
When I import them, they are on AutoCAD layers which have a color assigned to them (the objects are all white). Using this script I color the objects according to their layers. If I then add them to one layer (let’s say floorplan groundfloor) they keep the color of the AutoCAD layers.
However, since there are thousands of small spline objects the scene really slows down, once you unhide one of the floorplans. unhiding more then one or two pretty surely freezes the machine.
So what I need to do (and I do that by hand, and its tedious and annoying) is select all objects of one color, isolate them, convert them into an editable spline, select the first in the list and attach all others to it.
Since this is a highly repetative work I do there, there must be a way to script this. something like:


attach multiple splines for selected object 
where wire color equals wire color of selected object

My problem is, I’ve got no idea how to translate that into MaxScript :shrug:

Any help would be highly apprecciated and may earn a

9 Replies

I think you can resolve this without resorting to maxscript. When you open the Import DWG dialogue box there is a drop down under the title ‘Derive AutoCAD Primitives by’. What do you have this set to? And if you set it to ‘Layer’ does this do what you want?

1 Reply
(@holzi)
Joined: 11 months ago

Posts: 0

It was set to entity so i don’t have to clean up the DWG in AutoCAD first. But Changing to layer did not solve this. still have 244 objects on one layer for example.
Though it seems it is a little better then entity

OK then we do need a little maxscript:

(
	fn attachByColour spl =
	(
		local splCol = spl.wirecolor
		local sceneSplines = for o in getClassInstances splineShape collect refs.dependentNodes o firstOnly:true
		
		for s in sceneSplines do
		(
			if s.wirecolor == splCol and spl != s do addAndWeld spl s 0
		)
		updateShape spl
	)
	if isKindOf $ splineShape do attachByColour $
)

If you select a spline and run this then it should attach all others in the scene with the same colour.

Also this may be helpful

here is how i do it…


    fn attachSplines sp splines: = if canconvertto sp SplineShape do
    (
    	if splines == unsupplied do splines = (for node in shapes where canconvertto node SplineShape collect node)
    	
    	converttosplineshape sp
    	for s in splines where s != sp do addandweld sp (converttosplineshape s) -1
    	sp
    )
    /*
    delete objects
    sp = circle radius:20
    for k=0 to 4 do (circle radius:10 pos:[10 + k*20,0,0])
    attachSplines sp
    */
    
there are couple trick there:
# i use [b]converttosplineshape[/b] to make the function undoable, and be working for all shape classes
# i use [b]negative threshold[/b] in [b]addandweld [/b]function which guaranties that vertices will never be welded

also it’s much faster to search in all shapes and check the class than use getclassinstances (which is a slow function)

Thanks gazybara, I’m sure that will be useful.

Thanks for the tips Denis, especially the converttosplineshape. I’ve made mine into a rollout and added a couple of your tips:

try destroyDialog attachSplByCol catch()

rollout attachSplByCol "Attach By Colour" width:160 height:80
(
	local sourceSpl
	
	fn isSpline obj = isKindOf obj splineShape
		
	fn attachByColour spl =
	(
		with undo "Attach Splines" on
		(
			converttosplineshape spl
			local splCol = spl.wirecolor
			local sceneSplines = for o in getClassInstances splineShape collect refs.dependentNodes o firstOnly:true
			if attachSplByCol.weld_cb.checked == true then weldThres = weldTh_sp.value else weldThres = -1
			
			for s in sceneSplines do
			(
				if s.wirecolor == splCol and spl != s do addAndWeld spl (converttosplineshape s) weldThres
			)
			updateShape spl
		)
	)
	
	pickButton selSource_pbt "Select First Spline" width:150 pos:[5,5] autoDisplay:true filter:isSpline
	checkBox weld_cb "Weld vertices?" pos:[10,32]
	spinner weldTh_sp width:50 pos:[105,32] range:[0,1000,0.1] enabled:false toolTip:"Weld vertices threshold"
	button attSpl_bt "Attach By Colour" width:150 pos:[5,55]
	
	on selSource_pbt picked obj do
	(
		sourceSpl = obj
		select sourceSpl
	)
	on selSource_pbt rightclick do selSource_pbt.object = undefined
	on weld_cb changed state do weldTh_sp.enabled = state
	on attSpl_bt pressed do
	(
		if sourceSpl != undefined do attachByColour sourceSpl
		selSource_pbt.object = undefined
	)
)
createDialog attachSplByCol

I’m not not sure the UI would pass this thread though

Probably not because of xpos of checkbox. Just kidding. Nicely done.

I actually inset it slightly deliberately because i thought it looked better. Try it at x pos = 5 and see what you think.

1 Reply
(@gazybara)
Joined: 11 months ago

Posts: 0

Yup. This is irrelevant, who cares. It is important that the tool works.